need help with simple rand problem 
Author Message
 need help with simple rand problem

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 be
greatly appreciated.
Ellen Drewes



Thu, 25 Mar 1999 03:00:00 GMT  
 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 */

- Show quoted text -

Quote:
}



Thu, 25 Mar 1999 03:00:00 GMT  
 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?

Ouch. If you're not familiar with basic data types, you need to read
some more before trying to code. While it's true that, like many
things, you learn to code by coding, you also need some background in
order to get anywhere.

An integer is a whole number (it has no fractional or decimal
component.) It's range depends upon the particular OS you're using.
(Most common OSs have either 2 byte or 4 byte integers, but that isn't
guaranteed.) Signed integers may hold negative numbers. Unsigned
integers may not hold negative numbers. If you're using 2 byte
integers (MSDOS systems do), an unsigned int is a number in the range
of 0 to 65,535.

The rand() function will return a psuedo random unsigned int. In order
to confine it to a smaller range, you use the % (modulus) operator.
You said you were familiar with that, so I'll not go into those
details other than to note that an equation in the form of
n = a + rand() % b will always return a psuedo random number from the
sequence a to (a+b). (More on this later.)

The rand() function is not truly random. It requires a seed to begin,
and will output the exact same sequence every time given the same
seed. It's therefore important to seed the function with a different
number each time. srand() works by seeding the function with the time
or a similar varying number.

The next thing you need to do is map your random number to the group
you're using. In your example, you were selecting from the first five
even number. You have five possibilities, so you'll need a random
number from 1 to 5:
n = 1 + rand() % 4;
To map this to your group {2, 4, 6, 8, 10}, merely multiply n by 2.
Of course, you can add this into your original equation:
n = 2 * (1 + rand() % 4);

If the group you're using does not have a discernable pattern, or the
pattern is too complex to map easily, consider using an array. (If
you're not familiar with arrays, look them up in your manual or
textbook.) For example, to select a random number from the group {1,
3, 4, 7, 12} you might do something like this:

int i[] = {1, 3, 4, 7, 12};

n = rand() % 4; /*Returns a random number from 0 to 4*/
printf("The number selected was %i"; i[n]);

Regards,               A faith that cannot survive collision
Daniel                 with the truth is not worth many



Thu, 01 Apr 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Simple problem across processes - need simplest answer

2. Need Help with RAND comparing or Arrays...

3. need help on srand and rand

4. Need help on rand(), etc.

5. Newbie needs help on simple problem

6. Need help on simple problem

7. Very Simple Problem - NEED HELP PLEASE

8. A Simple Problem, Need Help

9. Help with a rand() problem?

10. Simple question about rand()

11. simple rand questions

12. rand() in visual C differs from rand in unix

 

 
Powered by phpBB® Forum Software