Originally Posted by
jocdrew21
Is this counter productive because I am using the same time on the same computer? In other words, will they always be equal?
The code runs and stops. I play with it for a little while and thought I would seek some direction.
What did the output tell you? Are they always the same, or did your loop run several times, printing two different random numbers each iteration (except the last)?
srand just sets the seed, i.e. the starting point for the PRNG algorithm to generate it's numbers from. Every call to rand() after that returns a new random number. One thing about random number sequences (true or pseudo) that newbies often miss, is that numbers are allowed to repeat. You can have a sequence like 13, 4, 79, 16, 16, ...
Code:
for (i=0; i < NUMBER; i++) {
...
}
Those for loops are pointless, you generate 6 random numbers, but throw away the first 5, since only the sixth value is stored in randValue at the end of the loop.
Code:
randValue2= 1 + (int)rand() % 49;
That generates random values in the range of [1, 49]. You will not get 50, if that was your intention. x % N will give you a value between [0, N-1], so rand() % 49 will give you a value between [0, 48]. Adding 1 to that gives you a value in the range [1, 49]. For simple random numbers in a given range of [low, high], I use the following:
Code:
rand_range(int low, int high)
{
return (rand() % (high - low + 1)) + low;
}
So to get the same numbers you're getting now, [1, 49], you would call rand_range(1, 49). If you wanted to include 50, call rand_range(1, 50).
Code:
printf("%d%d",randValue,randValue2);
Put some bloody space between your values. How would you know if output of 125 was 12 and 5 or 1 and 25?