Random one
Code
/* File: rand1.c
Play with a random number generator.
*/
#include
#include
main ( )
{
int i, j ;
/* Print out 50 random numbers in a 10 x 5 grid */
for (i = 0 ; i < 10 ; i++)
{
for (j = 0 ; j < 5 ; j++)
{
printf("%8d ", rand() ) ;
}
printf("\n") ;
}
}
Run
lassie% cc rand1.c
lassie%
lassie% a.out
16838 5758 10113 17515 31051
5627 23010 7419 16212 4086
2749 12767 9084 12060 32225
17543 25089 21183 25137 25566
26966 4978 20495 10311 11367
30054 17031 13145 19882 25736
30524 28505 28394 22102 24851
19067 12754 11653 6561 27096
13628 15188 32085 4143 6967
31406 24165 13403 25562 24834
lassie%
lassie% a.out
16838 5758 10113 17515 31051
5627 23010 7419 16212 4086
2749 12767 9084 12060 32225
17543 25089 21183 25137 25566
26966 4978 20495 10311 11367
30054 17031 13145 19882 25736
30524 28505 28394 22102 24851
19067 12754 11653 6561 27096
13628 15188 32085 4143 6967
31406 24165 13403 25562 24834
lassie%
Seeding a random process
Most random number generators are actually pseudo-random number
generators.
They always generate the same random sequence.
This has advantages (e.g., debugging) as well as disadvantages (e.g.,
leads to boring games)
Elegant solution: provide a separate function to initialize the random
number generator -- a seed value.
How do we get "random" seed values? Aren't we just sweeping the
problem under the rug?
Usual trick -- use some external information, such as the current time
-- for the seed value.
Random two
Code
/* File: rand2.c - Play with a random number generator.
Second try.
*/
#include
#include
main ( )
{
int i, j, seed ;
/* Do the priming read */
printf("\nEnter random seed or -1 to end: ") ;
scanf("%d", &seed);
while (seed != -1)
{
/* Provide seed to random number generator */
srand(seed) ;
/* Print out 50 random numbers in a 10 x 5 grid */
for (i = 0 ; i < 10 ; i++)
{
for (j = 0 ; j < 5 ; j++)
{
printf("%8d ", rand() ) ;
}
printf("\n") ;
}
/* Get the next seed */
printf("\nEnter random seed or -1 to end: ") ;
scanf("%d", &seed);
}
}
Run
lassie% cc rand2.c
lassie% a.out
Enter random seed or -1 to end: 1
16838 5758 10113 17515 31051
5627 23010 7419 16212 4086
2749 12767 9084 12060 32225
17543 25089 21183 25137 25566
26966 4978 20495 10311 11367
30054 17031 13145 19882 25736
30524 28505 28394 22102 24851
19067 12754 11653 6561 27096
13628 15188 32085 4143 6967
31406 24165 13403 25562 24834
Enter random seed or -1 to end: 33214
16051 14425 24685 17726 13508
2044 20246 24889 3207 25483
29532 19187 7570 28699 9799
29486 5979 19628 12368 885
336 9749 11491 487 8376
7599 10645 26050 24647 28356
32037 26803 5508 32438 4660
8416 16347 17391 8644 12209
7553 12172 6514 4450 12812
1480 10611 22858 6862 20778
Enter random seed or -1 to end: 33214
16051 14425 24685 17726 13508
2044 20246 24889 3207 25483
29532 19187 7570 28699 9799
29486 5979 19628 12368 885
336 9749 11491 487 8376
7599 10645 26050 24647 28356
32037 26803 5508 32438 4660
8416 16347 17391 8644 12209
7553 12172 6514 4450 12812
1480 10611 22858 6862 20778
Enter random seed or -1 to end: -1
lassie%
What time is it?
man time
TIME(2) Silicon Graphics TIME(2)
NAME
time - get time
SYNOPSIS
#include
time_t time (time_t *tloc);
DESCRIPTION
time returns the value of time in seconds
since 00:00:00 GMT, January 1, 1970.
If tloc is non-zero, the return value is
also stored in the location to which tloc points.
SEE ALSO
stime(2), gettimeofday(3B).
WARNING
time fails and its actions are undefined if
tloc points to an illegal address.
DIAGNOSTICS
Upon successful completion, time returns the value of time.
Otherwise, a value of -1 is returned and errno is set to indicate the
error.
Page 1 Release 4.0.5 May 1992
Random three
Code
/* File: rand3.c
Play with a random number generator.
Third try. Using time to set the seed.
*/
#include
#include
#include
main ( )
{
int i, j, goAhead, timeSeed ;
/* Do priming read */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
while (goAhead == 1)
{
/* Use the time function to set the seed. */
timeSeed = (int) time(0) ;
printf("Setting seed to be: %d\n", timeSeed) ;
srand(timeSeed) ;
/* Print out 50 random numbers in a 10 x 5 grid */
for (i = 0 ; i < 10 ; i++)
{
for (j = 0 ; j < 5 ; j++)
{
printf("%8d ", rand() ) ;
}
printf("\n") ;
}
/* Get next input */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
}
}
Run
lassie% cc rand3.c
lassie% a.out
Enter 1 to continue: 1
Setting seed to be: 765137661
6014 4093 3363 29706 14610
10708 1853 15118 29801 16508
5556 10321 31567 26594 21974
14011 32385 6903 18736 11883
3064 2566 15035 8307 1526
29400 19141 25173 32465 24201
19873 17729 1410 24872 26894
1292 12013 14098 7272 10992
26606 23563 8080 24582 23591
30753 17564 27524 32140 15937
Enter 1 to continue: 1
Setting seed to be: 765137662
22852 21151 3489 25105 9395
32176 8819 2796 1123 12184
28812 28805 2224 28897 344
5680 25106 1853 28661 19788
9534 32121 12465 27915 13564
15906 21576 15106 28103 11508
17114 20591 28454 27398 10926
2126 7885 12728 7849 16922
16754 28760 8332 1379 17777
6295 4849 21013 6943 17838
Enter 1 to continue: 1
Setting seed to be: 765137663
6922 5442 3614 20503 4180
20877 15784 23242 5213 7861
19300 14520 5648 31199 11482
30116 17828 29572 5818 27693
16005 28908 9896 14755 25603
2411 24011 5038 23740 31582
14356 23453 22730 29924 27726
2959 3757 11357 8426 22852
6903 1188 8584 10944 11962
14604 24901 14501 14513 19739
Enter 1 to continue: 0
lassie%
Random four
Code
/* File: rand4.c
Play with a random number generator.
Fourth try. Using time to set the seed.
Also, generate numbers between 1 and 6.
*/
#include
#include
#include
main ( )
{
int i, j, goAhead, timeSeed ;
int r ;
/* Get initial input from user */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
while (goAhead == 1)
{
/* Use the time function to set the seed. */
timeSeed = (int) time(0) ;
printf("Setting seed to be: %d\n", timeSeed) ;
srand(timeSeed) ;
/* Print out 50 random numbers in a 10 x 5 grid */
for (i = 0 ; i < 10 ; i++)
{
for (j = 0 ; j < 5 ; j++)
{
r = rand() % 6 + 1 ;
printf("%8d ", r ) ;
}
printf("\n") ;
}
/* Get next input from user */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
}
}
Run
lassie% cc rand4.c
lassie% a.out
Enter 1 to continue: 1
Setting seed to be: 765137924
3 6 5 2 4
3 1 5 5 6
5 5 6 1 4
4 5 6 3 3
1 3 2 2 4
3 3 6 6 4
4 5 6 6 4
2 4 2 3 5
1 2 3 2 6
2 4 3 6 1
Enter 1 to continue: 1
Setting seed to be: 765137925
6 1 4 2 3
2 4 1 3 3
3 6 5 5 6
1 4 2 4 4
3 6 1 6 4
2 2 6 6 6
6 5 2 6 2
2 4 6 4 5
1 2 3 3 1
1 3 1 4 6
Enter 1 to continue: 1
Setting seed to be: 765137933
4 4 2 2 6
1 2 4 4 5
3 6 2 3 4
1 2 1 6 1
2 6 2 6 2
1 4 5 3 2
2 4 5 1 2
3 1 6 6 1
4 3 1 6 2
3 3 1 1 2
Enter 1 to continue: 0
lassie%
Random five
Code
/* File: rand5.c
Play with a random number generator.
Fifth try. Using time to set the seed.
Also, generate numbers between 1 and 6.
And, put code in functions.
*/
#include
#include
#include
#define LOW 1
#define HIGH 6
void SetRandomSeed(void) ;
int GetRandomNumber(void) ;
main ( )
{
int i, j, goAhead ;
int r1, r2 ;
/* Get initial input from user */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
while (goAhead == 1)
{
SetRandomSeed () ;
/* Print out 50 random numbers in a 10 x 5 grid */
printf("Sum of two dice between %d and %d\n", LOW, HIGH) ;
for (i = 0 ; i < 10 ; i++)
{
for (j = 0 ; j < 5 ; j++)
{
r1 = GetRandomNumber () ;
r2 = GetRandomNumber () ;
printf("%8d ", r1 + r2 ) ;
}
printf("\n") ;
}
/* Get next input from user */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
}
}
/* GetRandomNumber returns a random number within
* the range of LOW to HIGH, which must be #defined
* constants in the same file as the function or in
* a #included .h file.
*/
int GetRandomNumber (void)
{
int r ;
/* Call rand() to get a large random number. */
r = rand() ;
/* Scale the random number within range. */
r = r % (HIGH - LOW + 1) + LOW ;
return (r) ;
}
/* SetRandomSeed is a procedure. It uses the
* time function to seed the random number
* generator
*/
void SetRandomSeed (void)
{
int timeSeed ;
/* Use the time function to set the seed. */
timeSeed = (int) time(0) ;
srand(timeSeed) ;
}
Run
lassie% cc rand5.c
lassie% a.out
Enter 1 to continue: 1
Sum of two dice between 1 and 6
3 8 6 8 7
7 8 7 5 6
10 4 2 8 3
6 9 11 9 7
3 6 8 3 7
6 5 8 7 4
4 8 4 7 3
11 7 9 10 5
9 7 7 7 9
12 5 8 7 5
Enter 1 to continue: 1
Sum of two dice between 1 and 6
6 3 7 3 9
8 5 5 4 11
6 3 7 5 4
9 9 6 3 6
8 8 7 7 9
7 6 7 4 7
7 3 9 11 9
5 8 7 3 10
10 7 4 7 7
10 4 4 8 6
Enter 1 to continue: 1
Sum of two dice between 1 and 6
5 12 10 2 9
6 10 6 10 11
5 4 9 7 8
3 6 6 4 2
9 2 3 9 9
7 9 10 6 8
11 8 10 7 9
7 7 5 9 7
7 6 7 10 7
6 7 3 7 10
Enter 1 to continue: 0
lassie%
CSEE
|
201
|
201 F'98
|
lectures
|
news
|
help