rand() not so random....

This is a discussion on rand() not so random.... within the C++ Programming forums, part of the General Programming Boards category; Everytime I run this program, that is supposed to display a random number between 0 and 20, it outputs 20. ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    rand() not so random....

    Everytime I run this program, that is supposed to display a random number between 0 and 20, it outputs 20. I would really appreciate if somebody could point out were I'm going wrong.

    Code:
    #include <iostream.h>
    #include <cstdlib.h>
    
    
    using namespace std;
    
    int main()
    {
    
     int mynumber = rand() % 10 + 20;
    
     cout <<  mynumber;
    
     cin.get();
    
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    srand( time(0) );

    before rand();

    FAQ

    Unrelated, but use <iostream>, not <iostream.h>

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Your program is using default seed values to generate rands. To produce real random values the best method is using the ctime library; this way since no two time values are the same you get perfectly random numbers.

    Add this to your code:
    Code:
    #include <ctime>
    srand((unsigned)time(0));

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    To produce real random values the best method is using the ctime library; this way since no two time values are the same you get perfectly random numbers.
    Not quite. Since time is ever-changing, using the time is a good way to generate a random number seed, such as that passed to srand. Using the time for a random number is a bad idea.

    And anyway, rand() is a pseudo-random number generator -- it doesn't generate perfectly random numbers. It's very hard for computers to generate random numbers in general, and non-pseudo-random numbers in particular.

    For most cases pseudo-random numbers a sufficient however.

    [edit]
    that is supposed to display a random number between 0 and 20
    Well, it won't.
    Code:
    int mynumber = rand() % 10 + 20;
    Read that as "Generate a random number, divide it by 10 and take the remainder (which will be from 0 to 9), then add 20." Accordingly, you should get a number from 20+0 to 20+9 -- 20-29 inclusive.

    If you want a random number from 0 to 20 inclusive use
    Code:
    rand() % 21
    [/edit]
    Last edited by dwks; 01-20-2007 at 04:25 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    rand() is also pretty poor randomness on most compilers; most use some version of a linear congruential generator which is one of the worst (in terms of randomness) performing PRNGs. If you're making a professional program, try something like boost's mersenne twister PRNGs. Of course rand() works fine for simple things and testing, or where you don't care too much about how random things are.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    You might want to be eternally confuzzled by Prelude concerning rand() before you take dwks' advice.
    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

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    9

    Seeding..

    What you have to do is seed the random number generator. Just do a google search for seed and rand() and you will get a pile of great links...

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    here is a great way to generate random numbers:
    Code:
    int getRandomInt( int iMin, int iMax )
    {
    	return static_cast<int>( (iMax - iMin) * double(rand())/(RAND_MAX-1) + iMin );
    }

    Change it to use whatever type you like

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by avalanche333
    here is a great way
    Not so great... read link posted by laserlight
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    avalanche's method is basically the one that laserlight's link recommended, apart from the fact it should be +1 and not -1 added to RAND_MAX.

    However, any LCG will always have a stronger degree of serial correlation than many other PRNGs. This: http://upload.wikimedia.org/wikipedia/en/3/38/Randu.png is an example of a particularly poor (and once, a highly popular) LCG; when it generates triples of random points, all the points lie on one of fifteen different planes in the 3d space. Most LCGs are not as bad as that, but they all have the same phenomena, caused by a stronger than desired correlation between one output and the next subsequent output.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    Thanks for all the help everyone. I've been trying out a few of the above and most work very well. Thanks again.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Not so great... read link posted by laserlight
    Same to you. A solution doesn't have to be exactly the same to follow the same principles.

    >apart from the fact it should be +1 and not -1 added to RAND_MAX
    +1.0 would be more likely to avoid integer overflow if RAND_MAX is the same as INT_MAX.
    My best code is written with the delete key.

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