Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Random Numbers

    Hi,

    I have written a program that uses the random number generator function from the GNU scientific library.
    Every time I execute the program it generates the same random numbers. Is there anyway I can generate different random number every time I execute the program.

    Thanks
    Vibhuti

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably need to seed the random number generator with a different seed.
    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

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    To go even further, you could provide a way for the user to specify the seed, either as a command line argument, environment variable, or something else. That way, the user can reproduce a result if they want to.

    (It also makes debugging less impossible if you have a way to reproduce the seed)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Most random number generators work on a seed. In the normal C library random generator, that's srand(). I don't know what it's called in GSL, but google finds this:
    http://www.gnu.org/software/gsl/manu...alization.html

    --
    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.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Thank you!! Changing the seed works

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    I have the same problem.
    But i do not understand how u change the seed

    can someone please give me more info on that
    im still very new to C. :-)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cursedninja
    But i do not understand how u change the seed
    According to the documentation that matsp linked to, it is by using the gsl_rng_set() function. Have you tried using it? (Or if you are talking about the C standard library's rand() function, it will be srand(), also mentioned by matsp.)
    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

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    I did not understand much about the gsl_rng_set() function.

    i am using the standard library function.
    How do i change the seed for the srand?

    could u just show me what code i should use?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cursedninja
    i am using the standard library function.
    How do i change the seed for the srand?
    Read this article on using rand().
    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

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Smile

    thank you very much.

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. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM