How to generate random numbers the same everytime

This is a discussion on How to generate random numbers the same everytime within the C++ Programming forums, part of the General Programming Boards category; Hi, How can I generate the same random numbers everytime I run my program? And not the same everytime? Does ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    How to generate random numbers the same everytime

    Hi,
    How can I generate the same random numbers everytime I run my program? And not the same everytime? Does it have something to do with the seed for the random number generator? I am using rand().
    Thanks and regards!

  2. #2
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Quote Originally Posted by lehe View Post
    ... the same random numbers everytime I run my program? And not the same everytime?
    I'm not sure that makes sense. Could you please clarify.
    IDE - Visual Studio 2005
    Windows XP Pro

  3. #3
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    I'm not exactly sure if I understood your question, but here's my shot:

    Code:
    int seed = someNumber;
    srand(seed); // Set the seed
    
    cout << rand() <<endl; 
    cout << rand() <<endl;
    cout << rand() <<endl;
    If you execute this program several times, you will have the exact same output.


    So the rand() function gives random numbers in a sequence, based on a seed.
    If you want random sequences, you will have to use a "random" seed.
    You can do this by, for example, using the current time as the seed.

    For more info, see:
    rand - C++ Reference
    Last edited by Drogin; 04-09-2009 at 07:02 PM.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Similar to Drogin:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    int main() {
       int theNumber = ( 1 + rand() % 10 );
    
       std::cout << theNumber << std::endl;
    
       return 0;
    }
    You do not include srand() or <ctime> at all. If rand() is not seeded, it will produce the same results each time. Place a loop around your code to see the desired effects.
    Last edited by swgh; 04-09-2009 at 02:21 PM. Reason: Members name spelt wrong
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so rand() and srand() does something like this (it is not necessarily how it does it in detail, but the PRINCIPLE applies - the actual code may be a bit more complicated):
    Code:
    static seed = largeNumber0;
    int rand()
    {
       seed = seed * largenumber1 + largenumber2;
       return seed % MAX_RAND;
    }
    
    void srand(int x)
    {
       seed = x;
    }
    So if seed is the same value, then the random numbers will be the same ones every time.
    The large numbers are almost always prime numbers or pseudo-primes (that is "nearly primes").

    In fact, the SEQUENCE of numbers you get out of rand() will ALWAYS be the same, it's just where in the [hopefully pretty long] sequence you start that will change by modifying the seed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    A possibly valuable addition:
    These methods work, but are not portable. Another implementation of srand/rand may be different. If you want it to work portably, implement your own random number generator (there are several good algorithms available on the web)

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    For example, the Mersenne Twister.

    It has a free reference implementation, too, that is very easy to use.

  8. #8
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Could someone explain why it's not portable?
    I thought this function was defined in the C standard library, so I guess the definition in the standard is so strict it enforces portability anyway?
    I mean, what else would standards be good for...

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    C standard says rand must give you a sequence of pseudo-random numbers based on a seed. Implementors get to choose the actual algorithm.

  10. #10
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Ye, but how can the implementation bother our portability, as long as the implementation stay true to the ISO Standard?

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    It doesn't. As long as you don't make the assumption that the same seed will give you the same sequence everywhere. That is not gauranteed by the standard.

  12. #12
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    As long as you don't make the assumption that the same seed will give you the same sequence everywhere. That is not gauranteed by the standard.
    Aaah, I'm with you now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 01:00 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  4. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 01:29 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 05:11 PM

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