Rand() Example
Rand3 Example from Lecture 4
/*********************************************
* File: rand3.c
* Author: S. Bogar
* Date: 5/5/95
* SSN:123-45-6789
* Section: 0101
* Email: bogar@cs.umbc.edu
*
* More fun 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);
}
}
Output:
umbc9[348]% cc rand3.c
umbc9[349]% 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
Last Modified 26 September 2000