A better random fuction

This is a discussion on A better random fuction within the C++ Programming forums, part of the General Programming Boards category; the current rand() is only a signed short does anyone know a better randmizer that would return a UINT or ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,453
    You can try the Mersenne Twister algorithm.
    Someone has implemented it in C++ here:
    http://www-personal.engin.umich.edu/...neTwister.html
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21