Professional Documents
Culture Documents
Random
Random
This week we’re going to generate some random numbers. We need the
function
rand()
#include <iostream.h>
#include <stdlib.h>
int main()
{
int i,seed;
srand(seed);
return 0;
}
The function srand() is used to start the series of. It takes an argument
known as a “seed” which is an integer. The above program prompts you to
enter the seed at the beginning. If you run the program twice and give it the
same seed each time you will get the same series of random numbers. If you
give it a different seed, you will get a different series.
• Adapt the above program to find the mean of 1000 random numbers. What
value would you expect this to be?
• Run your program several times with different seeds. How does the value
of the mean vary?
• Change the program so that it finds the mean of 10000 numbers. Does the
mean get closer to its expected value?
Try modelling Darwinian evolution. Design your own animals and display
them using the graphics functions. The characteristics of your animals (e.g.
the length of their legs, the size of their heads etc. ) should be determined by a
set of “genes”. Let these genes undergo random variation from one generation
to another. By selecting animals with certain characteristics at each
generation you can get them to evolve.