
need help with simple rand problem
Quote:
>I've just begun learning C, and I'm having a few problems. First,
>I'm having some difficulty understanding the math library function
>`rand', especially scaling and shifting it. I understand the general
>equation for rand, n = a + rand() % b, but this seems to only work for a
>consecutive range of numbers. How would you go about picking a random
>number from a set? example: my homework problem, where I am supposed to
>write a statement that will print a number at random from a set like
>{2,3,6,8,10}, which increases in increments of 2.
> Also, the srand function is making no sense to me. According to
>my textbook, srand takes an unsigned integer argument and seeds the rand
>function-- but what exactly is an unsigned integer, and what is seeding?
>are there any general guidlines for using srand?
> I know these are very basic questions, but any help would
>greatly appreciated.
>Ellen Drewes
There are several questions here:
First, to random select a number from a set, just select the index at
random. Since the statement
rand() % n
generates a random number between 0 and n - 1, it follows that one of the
numbers array[rand()%5] will randomly select one number from array[0],
array[1], array[2], array[3], array[4] (five numbers).
Next, note that "seeding" refers to the fact that the numbers generated by
rand() aren't really random. They LOOK random, but actually they have a
pattern in the sense that they eventually repeat because they are generated
from a congruence of the form
x[i + 1] = a * x[i] mod m
EXAMPLE: Generate pseudorandom numbers using a = 13, m = 100.
Ans. x[0] = 1
x[1] = 13 * 1 mod 100, so x[1] = 13
x[2] = 13 * 13 mod 100, so x[2] = 169 = 69 mod 100
x[3] = 13 * 69 mod 100, so x[3] = 897 = 97 mod 100
x[4] = 13 * 97 mod 100, so x[4] = 1261 = 61 mod 100
....................................................
Thus, the pseudorandom numbers are {1, 13, 69, 97, ...}
There are a total of twenty numbers in the sequence (the
last is 77); since 13 is prime, the sequence will repeat
indefinitely.
The rand() function uses much larger numbers so that the
sequence will take much longer to repeat. Calling rand()
automatically sets x[0] = 1 in the above sequence; calling
srand(unsigned int seed) sets x[0] = seed, so begins in a
different point of the sequence. Thus, setting the seed
equal to 1 in srand() should behave in exactly the same way
as rand(), since in both cases x[0] = 1 (try it!)
Finally, 'unsigned' means exactly what it sounds like. The
advantage of using unsigned integers is to extend the range
of acceptable values; for instance, whereas a 16-bit integer
can range from -32768 to +32767, a 16-bit unsigned integer can
assume positive values 0 to +65535.
I've enclosed a small program to illustrate these concepts. Good luck!
-- Michael
#include <stdio.h>
#include <stdlib.h> /* includes rand() and srand() functions */
main()
{
int array[5] = {2, 4, 6, 8, 10}; /* elements array[0] to array [4] */
int i;
unsigned int seed = 29; /* try different unsigned integers here, and the
output for the second loop will vary */
printf("Printing random index three times using rand()...\n");
for(i = 1; i <= 3; i++)
printf("%i\n", array[rand() % 5]); /* index is a random number 0..4 */
printf("\nPrinting random index using srand()...\n");
srand(seed); /* no return value, simply re-initializes rand() */
for(i = 1; i<= 3; i++)
printf("%i\n", array[rand() % 5]); /* index is a random number 0..4 */
Quote:
}