-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbug2new.c
64 lines (57 loc) · 1.64 KB
/
bug2new.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/* This program is supposed to test if you get the same random numbers
if you reset the random number generator with a certain seed
value. */
int main(void){
int i, j, seed, failed, tested;
int **mybuffer;
printf("Please enter a seed for the random number generator (any integer): ");
scanf("%d",&seed);
srand(seed);
/* Allocate a bunch of pointers */
mybuffer = calloc(100,sizeof(int *));
if (mybuffer == NULL){
fprintf(stderr, "Failed to allocate memory!");
exit (-1);
}
/* Allocate memory to each pointer */
for (i=0 ; i<100 ; i++){
mybuffer[i] = calloc(10,sizeof(int));
if (mybuffer[i] == NULL){
fprintf(stderr, "Failed to allocate memory!");
exit (-1);
}
}
/* Fill the allocated memory with random numbers */
for (i=0 ; i<100 ; i++){
for (j=0 ; j<10 ; j++){
mybuffer[i][j] = rand();
}
}
/* Lets try to see if we get the same random numbers again. */
/* Reset the seed to the random number generator. */
srand(seed);
failed = 0;
tested = 0;
for (i=0 ; i<100 ; i++) {
for (j = 0; j < 10; j++) {
if (mybuffer[i][j] != rand()) {
printf("Random number mismatch!\n");
failed = 1;
}
tested++;
}
}
if (failed)
printf("Something went wrong...\n");
else
printf("%d number verified and correct\n",tested);
/* Release the memory */
for (i=0 ; i<100; i++){
free(mybuffer[i]);
}
free(mybuffer);
return 1;
}