Home > Back-end >  Beginners on problem of statistics of random Numbers
Beginners on problem of statistics of random Numbers

Time:11-17

Recently read Song Jinsha C language learning materials, which produces a random number of examples below
#include
# define N 100000

Int a [N].
Void gen_random (int upper_bound)
{
int i;
for(i=0; iA [I]=rand () % upper_bound;
}
Int howmany (int value)
{
Int count=0, I;
For (I=0; iIf (a==[I] value)
++count;
Return the count.
}
Int main (void)
{
int i;
Gen_random (10);
Printf (" value \ thow the things \ n ");
for(i=0; i<10; I++)
Printf (" % d % d \ t \ n ", I, howmany (I));
return 0;
}
The statistical results are as follows: always
The value how many
0, 10130
1 10072
2 9990
3 9842
4 10174
5 9930
6 10059
7 9954
8 9891
9 9958
It's the same on my computer so, information... Really is random, constant why statistics?

CodePudding user response:

https://bbs.csdn.net/topics/390659274

CodePudding user response:

Pseudo random number, and set up a seed, to see if this is feasible,

 # include & lt; stdlib.h> 
#include
# define N 100000

Int a [N].
Void gen_random (int upper_bound)
{
int i;

Srand ((unsigned int) time (NULL));

for(i=0; iA [I]=rand () % upper_bound;
}
Int howmany (int value)
{
Int count=0, I;
For (I=0; iIf (a==[I] value)
++count;
Return the count.
}
Int main (void)
{
int i;
Gen_random (10);
Printf (" value \ thow the things \ n ");
for(i=0; i<10; I++)
Printf (" % d % d \ t \ n ", I, howmany (I));
return 0;
}

CodePudding user response:

Possible, crab crab,

CodePudding user response:

 void gen_random (int upper_bound) 
{
int i;

Srand (time (NULL);//generates a seed ~
for(i=0; iA [I]=rand () % upper_bound;
}
  • Related