Random Numbers
Random numbers
are an important concept in mathematics and statistics and have many
applications in almost all fields of science and engineering.
Random one
Code
/*********************************************
** File: rand1.c
** Author: S. Bogar
** Date: 3/3/94
** Section: 0101
** Email: bogar@cs.umbc.edu
**
** Examples with a random number generator.
**********************************************/
#include
/* needed for function rand() */
#include
int 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("%11d ", rand() ) ;
}
printf("\n") ;
}
return 0;
}
Run
linux3[116] % gcc -Wall -ansi foo.c
linux3[117] % a.out
1804289383 846930886 1681692777 1714636915 1957747793
424238335 719885386 1649760492 596516649 1189641421
1025202362 1350490027 783368690 1102520059 2044897763
1967513926 1365180540 1540383426 304089172 1303455736
35005211 521595368 294702567 1726956429 336465782
861021530 278722862 233665123 2145174067 468703135
1101513929 1801979802 1315634022 635723058 1369133069
1125898167 1059961393 2089018456 628175011 1656478042
1131176229 1653377373 859484421 1914544919 608413784
756898537 1734575198 1973594324 149798315 2038664370
linux3[118] % !a
a.out
1804289383 846930886 1681692777 1714636915 1957747793
424238335 719885386 1649760492 596516649 1189641421
1025202362 1350490027 783368690 1102520059 2044897763
1967513926 1365180540 1540383426 304089172 1303455736
35005211 521595368 294702567 1726956429 336465782
861021530 278722862 233665123 2145174067 468703135
1101513929 1801979802 1315634022 635723058 1369133069
1125898167 1059961393 2089018456 628175011 1656478042
1131176229 1653377373 859484421 1914544919 608413784
756898537 1734575198 1973594324 149798315 2038664370
linux3[119] %
Seeding a random process
Most random number generators are actually pseudo-random number
generators.
They always generate the same random sequence as shown in the
example runs above.
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. Using a different seed value
generates a different sequence of random values.
How do we get "random" seed values? Aren't we just sweeping the
problem under the rug?
Usual trick -- use some external information
Random two
Code
/********************************************
* File: rand2.c
* Author: S. Bogar
* Date: 5/5/95
* Section: 0101
* Email: bogar@cs.umbc.edu
*
* Play with a random number generator.
* Second try. - Ask the user for a seed value
*********************************************/
#include
#include
int 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("%10d ", rand() ) ;
}
printf("\n") ;
}
/* Get the next seed */
printf("\nEnter random seed or -1 to end: ") ;
scanf("%d", &seed);
}
return 0;
}
Run
linux3[119] % gcc -Wall -ansi rand2.c
linux3[120] % a.out
Enter random seed or -1 to end: 1
1804289383 846930886 1681692777 1714636915 1957747793
424238335 719885386 1649760492 596516649 1189641421
1025202362 1350490027 783368690 1102520059 2044897763
1967513926 1365180540 1540383426 304089172 1303455736
35005211 521595368 294702567 1726956429 336465782
861021530 278722862 233665123 2145174067 468703135
1101513929 1801979802 1315634022 635723058 1369133069
1125898167 1059961393 2089018456 628175011 1656478042
1131176229 1653377373 859484421 1914544919 608413784
756898537 1734575198 1973594324 149798315 2038664370
Enter random seed or -1 to end: 33214
432216650 1875463247 1337819939 96688283 655508831
945160550 893940874 1947594271 1545193778 1798239982
2108503877 180194735 640992288 691307416 1550595792
1700036512 1139882851 1461754528 333981282 404534183
988189379 1756042718 927320035 378105668 1794442803
466227841 301998360 1283344932 1255397960 2058688227
1675713424 1687614610 1786667826 866049715 1784302893
294693010 1811210265 530760120 94803633 1208920395
181516454 55823862 1389115131 822508742 747131279
792227275 375061606 1887014130 106498155 709042889
Enter random seed or -1 to end: 33214
432216650 1875463247 1337819939 96688283 655508831
945160550 893940874 1947594271 1545193778 1798239982
2108503877 180194735 640992288 691307416 1550595792
1700036512 1139882851 1461754528 333981282 404534183
988189379 1756042718 927320035 378105668 1794442803
466227841 301998360 1283344932 1255397960 2058688227
1675713424 1687614610 1786667826 866049715 1784302893
294693010 1811210265 530760120 94803633 1208920395
181516454 55823862 1389115131 822508742 747131279
792227275 375061606 1887014130 106498155 709042889
Enter random seed or -1 to end: -1
linux3[121] %
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
* Author: S. Bogar
* Date: 5/5/95
* 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
int 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("%10d ", rand() ) ;
}
printf("\n") ;
}
/* Get next input */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
}
return 0;
}
Run
linux3[121] % gcc -Wall -ansi rand3.c
linux3[122] % a.out
Enter 1 to continue: 1
Setting seed to be: 999902819
1812811358 246321345 71076061 1381352137 1224736995
746827981 1292305594 1510148943 470193638 2008585232
1172542431 1107754109 1997037143 748651806 1818813640
1590763204 125602580 1076880803 2025654214 987289684
2075560130 1973367735 698911611 2063555543 1394476957
1316978677 1409432497 1048853788 732759687 55397121
1857975976 398087397 301718466 1929052037 1779439534
1526455462 528396371 924261480 889120757 998590009
785363064 2061663188 2106344118 634916559 662831346
1777674110 78196116 788433926 707071265 2103850330
Enter 1 to continue: 1
Setting seed to be: 999902820
1505267462 1128781826 1789240121 2064634829 3959702
140674895 988805178 2137473239 44550271 36779635
1185625180 1445944376 703841914 851407791 1789492472
1494888745 505800752 557232250 281941200 249686321
67568403 465607823 47384873 694238041 1870917702
1256918008 763958294 1448386630 913066179 703204554
2082331111 270849994 1831986380 1724087584 188001175
1835946082 1864762479 1176806353 1825935674 1909312751
1213585988 864077206 1207773479 1917427902 1715484997
849782303 1264832999 73802101 1407014554 1546774200
Enter 1 to continue: 1
Setting seed to be: 999902821
127051626 2013600435 1366005144 1678141941 2007057787
615338077 1764138626 620331082 1774563693 218951654
128454171 719778998 1566355518 958263247 697149193
1409359826 1964640236 49764984 1772375339 1665516631
1294582981 1121358580 1550671767 414194431 219616400
1205996284 135938437 1872071308 1104874018 1370587120
1262294603 1231925645 1236703907 480816100 762583938
1096278046 1096154177 379238917 1716609128 723234222
598190571 1845063299 1443013220 17062441 655842898
2140162413 1426422267 472999487 42443749 1051313959
Enter 1 to continue: 0
linux3[123] %
Random four
Code
/********************************************
* File: rand4.c
* Author: S. Bogar
* Date: 5/5/95
* Section: 0101
* Email: bogar@cs.umbc.edu
*
* Play with a random number generator.
* Fourth try. Using time to set the seed.
* Also, generate numbers between 1 and 6.
*********************************************/
#include
#include
#include
int 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++)
{
/* modifies random value to be in the range
of 1 - 6, inclusive */
r = rand() % 6 + 1 ;
printf("%10d ", r ) ;
}
printf("\n") ;
}
/* Get next input from user */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
}
return 0;
}
Run
linux3[123] % gcc -Wall -ansi rand4.c
linux3[124] % a.out
Enter 1 to continue: 1
Setting seed to be: 999903116
2 4 5 1 1
1 2 1 5 5
5 5 5 4 5
1 1 4 4 3
3 5 5 1 2
4 5 1 2 2
1 2 6 4 2
4 2 1 5 1
5 1 5 3 2
1 3 2 4 4
Enter 1 to continue: 1
Setting seed to be: 999903117
5 4 3 2 5
3 5 1 2 4
3 4 6 3 6
6 2 5 2 1
6 6 2 5 3
1 2 6 5 4
6 1 2 6 2
6 6 5 6 1
2 6 2 1 6
2 6 1 4 5
Enter 1 to continue: 1
Setting seed to be: 999903119
4 2 1 3 4
1 1 5 2 4
2 3 5 5 3
2 5 4 1 4
6 6 5 2 4
6 5 4 3 3
4 4 4 4 4
1 4 2 4 3
6 3 4 2 1
6 2 3 1 6
Enter 1 to continue: 0
linux3[125] %
Random five
Code
/**********************************************
* File: rand5.c
* Author: Guess Who
* Date: You know when
* Section: Still the same
* Email: Don't think you can do this in your projects
*
* 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.
*********************************************/
/* header files */
#include
#include
#include
/* constants */
/* lowest "random" number to generate */
#define LOW 1
/* highest "random" number to generate*/
#define HIGH 6
/* function prototypes */
void SetRandomSeed(void) ;
int GetRandomNumber(void) ;
int 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 ");
printf("%d and %d\n", LOW, HIGH);
for (i = 0 ; i < 10 ; i++)
{
for (j = 0 ; j < 5 ; j++)
{
r1 = GetRandomNumber () ;
r2 = GetRandomNumber () ;
printf("%10d ", r1 + r2 ) ;
}
printf("\n") ;
}
/* Get next input from user */
printf("\nEnter 1 to continue: ") ;
scanf ("%d", &goAhead);
}
return 0;
}
/*********************************************
** Function: GetRandomNumber
** Usage: x = GetRandomNumber();
**
** Inputs: None
** Output: returns a random integer between LOW and
** HIGH, inclusive
**
** Assumptions:
** LOW and HIGH are #defines
** random number generator has been seeded
*********************************************/
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) ;
}
/*********************************************
** Function: SetRandomSeed
** Usage: SetRandomSeed();
**
** Inputs: None
** Output: the random number generator is seeded
** with time(). No value is returned
*********************************************/
void SetRandomSeed (void)
{
int timeSeed ;
/* Use the time function to set the seed. */
timeSeed = (int) time(0) ;
srand(timeSeed) ;
}
Run
linux3[125] % gcc -Wall -ansi rand5.c
linux3[126] % a.out
Enter 1 to continue: 1
Sum of two dice between 1 and 6
9 8 7 11 6
3 7 7 6 9
6 9 3 4 7
7 9 7 9 7
11 3 7 5 8
7 6 9 11 9
7 11 10 6 4
11 8 10 9 11
6 7 11 6 5
4 4 12 6 7
Enter 1 to continue: 1
Sum of two dice between 1 and 6
10 6 6 8 12
6 9 6 7 9
8 5 9 8 9
3 2 10 3 5
11 3 7 3 7
9 10 10 8 8
9 6 9 9 4
6 2 10 10 7
11 6 7 8 12
11 2 8 9 10
Enter 1 to continue: 1
Sum of two dice between 1 and 6
9 4 8 4 4
7 7 3 6 5
3 9 10 8 5
10 7 7 10 10
10 8 6 4 3
9 7 9 3 4
8 3 6 6 9
3 6 7 4 8
4 6 9 10 7
4 8 7 7 3
Enter 1 to continue: 0
linux3[127] %