程序代写 PowerPoint Presentation

PowerPoint Presentation

Assignment2

Copyright By PowCoder代写 加微信 powcoder

Random Numbers

• Computers are “deterministic” machines

• Computers are not capable of true
randomness

• Instead, pseudo-random numbers are used

– Pseudo-random numbers are generated using a
mathematical formula

– There is a pattern in pseudo-random numbers

– The pattern is nearly impossible to observe

Random in C++

• Functions provided:

– void srand(unsigned int seed);

• This function sets the seed for the random number

– int rand();

• This function generates and returns an integer value in
the range 0..RAND_MAX

Random in C++

• srand() is usually only called ONE time

• rand() is called every time a random number is

– If you want a number between 0 and N

• val = rand() % (N+1)

– If you want a number with a range M..N

• val = rand() % (N-M+1) + M

Enter seed: 12

Enter seed: 1652

Enter seed: 12

Note: Same seed = same sequence = same results

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com