Thread: A better random fuction

  1. #1
    Registered User Josh Kasten's Avatar
    Join Date
    Jul 2002
    Posts
    109

    A better random fuction

    the current rand() is only a signed short does anyone know a better randmizer that would return a UINT or float?
    int a; don't make a program without it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First RAND_MAX isn't guaranteed to be a short

    If you want a larger rand, then simply do something like rand() * rand()

    If you want a float in the range 0..1, then do (float)rand() / RAND_MAX, and scale accordingly
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Josh Kasten's Avatar
    Join Date
    Jul 2002
    Posts
    109

    Thanks

    thanks for the info
    int a; don't make a program without it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can try the Mersenne Twister algorithm.
    Someone has implemented it in C++ here:
    http://www-personal.engin.umich.edu/...neTwister.html
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM