Random number generation

This is a discussion on Random number generation within the C++ Programming forums, part of the General Programming Boards category; I know that the rand() function can produce random integers, but I want to produce non -integers in the range ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    12

    Random number generation

    I know that the rand() function can produce random integers, but I want to produce non-integers in the range of 0 to 1. Is there a function that will do this? Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ > Prelude's Corner > Random Numbers
    A few other useful operations are obtaining a random number between 0 and 1 where [0..1) or [0..1]. This can be done very easily using what we learned in the above code:
    Code:
    // Range [0..1)
    double r0 = (double)rand() / ( RAND_MAX + 1 );
    // Range [0..1]
    double r1 = (double)rand() / RAND_MAX;
    Last edited by Dave_Sinkula; 01-06-2005 at 01:37 PM. Reason: Added quote.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Note that if you use the time to seed the random number generator, you may want to skip past the first number. I noticed on mine that the first number was always above 0.95, but after that it is sufficently random.

    Also note that this method will only produce RAND_MAX + 1 different doubles. There are plenty of doubles that this method will not produce.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have no problems with this code seeded with time causing the first number to be over .95.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    12
    Thanks for all of your help by the way, your suggestion worked. Also, same here, my first number wasn't always 0.95 or above.
    Last edited by scrub05; 01-06-2005 at 03:34 PM.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    The 0.95 was just an example. Consider the following:
    Code:
    #include <ctime>
    #include <stdlib.h>
    #include <iostream>
    #include <windows.h>
    
    using std::endl;
    using std::cout;
    
    int main(int argc, char **argv)
    {
    	srand(time(NULL));
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	srand(time(NULL));
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	srand(time(NULL));
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	srand(time(NULL));
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	srand(time(NULL));
    	cout << ((double)rand() / RAND_MAX) << endl;
    	return 0;
    }
    This code produced the following output:
    Code:
    0.277657
    0.277779
    0.277871
    0.277963
    0.278054
    I realize that you (probably) don't want to seed your generator multiple times. However, a shrewd person may realize that the first random number will always be slightly larger than the one before it. For instance, I can predict that in 104 1/2 minutes, I'll be back around 0.9 again. Exploiting that kind of predictability should be simple. Skipping the first "random" number helps to avoid this problem.
    Code:
    include <ctime>
    #include <stdlib.h>
    #include <iostream>
    #include <windows.h>
    
    using std::endl;
    using std::cout;
    
    int main(int argc, char **argv)
    {
    	srand(time(NULL)); rand();
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	srand(time(NULL)); rand();
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	srand(time(NULL)); rand();
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	srand(time(NULL)); rand();
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	srand(time(NULL)); rand();
    	cout << ((double)rand() / RAND_MAX) << endl;
    	Sleep(1000);
    	return 0;
    }
    This code produced this output:
    Code:
    0.687948
    0.0159307
    0.343944
    0.671987
    1
    Still not wonderful, but better than the simple linear progression.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 04:16 PM
  3. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 04:23 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 07:40 AM
  5. Random Number Generation
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM

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