📄 10.txt
字号:
CS 1355
Introduction to Programming in C
originally scheduled for Thursday 2006.10.12 (Week 5)
made-up on Monday 2006.10.16 (Week 6)
Lecture notes (at http://r638-2.cs.nthu.edu.tw/C/notes/10.txt)
(continued from last time:
- by-reference vs by-value
- rand(), srand(), time()...)
* More experiments with the random number generator
Example from book: roll the dice 1000 times, count each face
(page 157-158, but paraphrased)
#include <stdio.h>
#include <stdlib.h>
int main() {
int f1=0, f2=0, f3=0, f4=0, f5=0, f6=0;
int roll;
int face;
for (roll=1; roll <= 6000; roll++) {
switch(face = 1 + rand() % 6) {
case 1: f1++; break;
case 2: f2++; break;
case 3: f3++; break;
case 4: f4++; break;
case 5: f5++; break;
case 6: f6++; break;
default: printf("error: should not happen: face=%d\n", face);
}
}
printf("%4s%13s\n%4d%13d\n%4d%13d\n%4d%13d\n%4d%13d\n%4d%13d\n%4d%13d\n",
"Face", "Frequency", 1, f1, 2, f2, 3, f3, 4, f4, 5, f5, 6, f6);
}
-----------
Output (on cs25; your results may vary on other platforms)
Face Frequency
1 945
2 983
3 1023
4 1015
5 1000
6 1034
Try the following code: command line interface
/*
* prompt for either a seed from the user, call srand();
* or generate the next sequence of n random numbers
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int cmd = '\0';
unsigned seed = 0;
int n = 10 /* by default */, i;
int minRandom = 0, maxRandom = 100; /* by default */
srand(seed);
while (cmd != 'q') {
if (cmd != ' ') { printf("* "); /* prompt */ }
cmd = getchar();
switch (cmd) {
case '?': /* help */
printf("\
s setRandomSeed\n\
g nRandomNumbers\n\
m minRandomNumber\n\
M maxRandomNumber\n\
t (uses timer to randomize)\n\
");
continue;
case 's': /* set random seed */
switch (scanf("%u", &seed)) {
case EOF:
cmd = 'q';
continue;
case 0:
printf("%u\n", seed);
continue;
}
/* otherwise, set the random seed */
srand(seed);
continue;
case 'g': /* generate the next n numbers */
if (scanf("%d", &n) == EOF) {
cmd = 'q';
continue;
}
/* now generate n random numbers */
for (i = 0; i < n; i++) {
printf("%d ", rand() % (maxRandom - minRandom + 1) + minRandom);
}
printf("\n");
continue;
case 'm': /* set the minimum random */
switch(scanf("%d", &minRandom)) {
case EOF: cmd = 'q'; continue;
case 0: printf("%d\n", minRandom);
case 1: continue;
}
case 'M': /* set the maximum random */
switch(scanf("%d", &maxRandom)) {
case EOF: cmd = 'q'; continue;
case 0: printf("%d\n", maxRandom);
case 1: /* successful, nothing else to do */
continue;
}
case 't':
srand(seed = time(NULL));
continue;
case ' ': case '\n': case '\t': case ';':
cmd = ' ';
continue;
case 'q': /* quit */
case EOF:
cmd = 'q';
}
}
}
-----
If you run it with the same seed,
it will generate the same sequence of random numbers
(result shown on cs25.cs.nthu.edu.tw)
% ./a.out
* g; <<< initially, seed is 0
0 56 90 99 64 60 87 46 2 27
* g;
40 85 3 62 79 19 48 74 62 87
* g;
94 10 37 39 79 75 52 83 4 5
* s 100 <<< set seed to 100
* g;
37 61 38 13 52 63 7 89 36 11
* g;
20 20 22 1 75 8 40 79 11 4
* s 0 <<< set seed to 0 again
* g;
0 56 90 99 64 60 87 46 2 27 <<< same sequence as initially!!
* g;
40 85 3 62 79 19 48 74 62 87
---------------
to randomize using timer:
./a.out
* t <<< randomize using timer
* g;
41 17 10 83 43 26 63 91 100 16
* g 20
81 66 51 37 27 21 66 95 1 51 58 25 91 14 98 36 44 38 73 43
* s; <<< display what the seed was
1160832289
---------------
you can use this program to roll dice
% ./a.out
* m 1 M 6 <<< this sets min = 1, max = 6
* * g; <<< generate n random numbers (default=10)
1 1 5 2 1 2 2 2 3 5
* g;
4 5 2 3 4 4 5 2 3 4
* g;
1 2 2 6 3 6 5 5 5 4
---------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -