Thread: Rand() questions

  1. #16
    Registered User
    Join Date
    Apr 2003
    "the above will produce trully random values of 1 -10."

    First of all, they are pseudo random numbers, and your method reduces their "randomness" because it effectively truncates the value returned from rand() to a few low order bits, which are not necessarily random because of the way pseudo random numbers are generated, so it's commonly villified as a poor method.
    Last edited by 7stud; 05-03-2003 at 11:16 PM.

  2. #17
    Registered User
    Join Date
    Apr 2003
    "Im so confused yes i did go to the links but can some one simplify the srand and time i dont get it"

    Ok, to get random numbers, you need to start with a seed for the random number generator. However, if your seed isn't random, then the numbers won't be random. It's kind of a catch-22.

    time(0) returns the current time in seconds from your system clock, so it's pretty random, and seems like a good way to seed the random number generator.

    You don't need to understand any of that. Just do this once in your program:


    before you use rand(), and you should be ok. However, I still find that if I run the program a few seconds later, I still get the same random number for the first number of a series of numbers, so it still isn't a fool proof system.

  3. #18
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Originally posted by dsig111
    [B]the srand function sets a seed to randomize off of and when using time to do it it will set the seed different ly each time and stop using % 11 it returns 0-10 snd when add 1 it returns 1-11...
    if thats true how come in my program it stoped a ten. Thanks guy i fixed my problem

  4. #19
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    time_t seconds;
    srand((unsigned int) seconds);
    int rnd;
    rnd=(rand() % 10+1);
    AIM: MarderIII

  5. #20
    Registered User
    Join Date
    Nov 2002
    The rand() function does not return a random number , it returns a pseudo-random number. This is evident when you get the same "random" number sequence every time you run your program. The way to prevent this is to seed the random number generator to produce a different sequence of random numbers. The srand() function seeds the random number generator to the given number. However, if you set the generator to the same seed number every time, you will still get the same sequence of random numbers. That is where the time comes in. Seeding the random number based on the time will give you the best randomness. A simple example:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    //Notice the use of C++ headers
    using namespace std;
    int main() {
    	//Seed random number generator based on time
    	for(int i = 0;i < 10;i++) 
    		cout <<rand() <<endl;
    Notice that it will most likely print off 10 really big numbers. Usually we only want to work with much smaller numbers, like 0-2(for 3 different outcomes). That's where the modulus operator(%) comes in. Hope that helps a little.

Popular pages Recent additions subscribe to a feed
Website Security Test