Thread: How to generate random numbers the same everytime

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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 06:02 PM.

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, 08: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, 05:20 AM
  4. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 12:29 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