float randvalue srandtimeNULL forint i1 irandnum i randvaluerand

# Float randvalue srandtimenull forint i1 irandnum i

This preview shows page 43 - 47 out of 47 pages.

float randvalue;srand(time(NULL));for(int i=1; i<=randnum; i++){randvalue=rand( ) ;cout<<setw(10)<<randvalue<<endl;}return 0;}
4.6 (Cont..)The argument srand( ) function is call to the time( ) function with a NULL argument. With this argument, the time ( ) function reads the computer’s internal clock time in second. The srand( ) then uses this time, converted to an unsigned int, to initialize the random number generator function rand ( ).
4.7Scaling of Random Numbers A random number generator can be scaled to a specific range such as 1 to 100. For example 1.To scale random number between 0.0 and 1.0 divide the return value of rand ( ) by RAND_MAX. It produces a floating-point number between 0.0 and 1.0.2.To scale random number as an integer between 0 and N use the following
4.7 (Cont..)rand ( ) % (N+1) orint(rand()/RAND_MAX *N)For example, int (rand ( ) / RAND_MAX * 100) produces a random integer between0 and 100. 3.To produce a random number between 1 and N, use the following1+rand ( ) % N For example 1+rand ( ) % 6 produces a random number between 1 and N simulating the roll of a dice.
4.7 (Cont..)In general, to produce a random number between aand buse the following scaling expressiona + rand ( ) % ( b+1-a)

#### You've reached the end of your free preview.

Want to read all 47 pages?

• Fall '18
• Heidar Malki
• Local variable, Global variable, static variable