PDA

View Full Version : about random number in C


newton1
Dec25-05, 12:10 PM
i generate a sequence of random number in [0,9], and i need to know whether the random number is uniform or not, so i should check how many
time of 1 or 2 or 3....had been generated by the generator???
but my program is not work!!???

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
FILE*fptr;
char fname[]="SRN.txt";
int i,j,y,a[100];
fptr=fopen(fname,"w");
srand (time(NULL));
for(j=0;j<10;j++)
{
for(y=0,i=0;i<10;i++)
{
a[i]=rand()%10;
if(j==a[i])y++;
printf("%d",a[i]);
fprintf(fptr,"%d\n",y);
y=0;
}
}
fclose(fptr);
}

and how can i create a uniform ramdom number generator

Hurkyl
Dec25-05, 01:14 PM
I don't think your problem is with C; I'm not entirely sure you know exactly what you're trying to do!

Try writing out in words what you are trying to do. Your goal is to write directions that are detailed enough that someone who has absolutely no idea what you're trying to do can follow the instructions.

neurocomp2003
Dec25-05, 11:59 PM
if y is your random numbe ryou reset it everytime...that will probably be your problem. Actually its problably the problem anyways. There are several books out there for random generators, pick up sedgewick's text. I'm sure you can find a standard random generator formula...www.mathworld.com

kant
Dec26-05, 07:12 AM
generate a sequence of random number in [0,9], and i need to know whether the random number is uniform or not, so i should check how many
time of 1 or 2 or 3....had been generated by the generator???
but my program is not work!!???

int j;
int k;
int array[10]={0};
printf(" enter size");
scanf("%d", size);
for( i=0; i< size; i++);
{
j=rand( )% 10; /*there is got to be something more*/
array[j]++;
}

newton1
Dec26-05, 08:20 PM
Thank you very much....
i know my ramdon generator is bad~~
i will find on some place...:smile:

jim mcnamara
Dec28-05, 04:43 PM
This article uses a tiny amount of math but is very clearly written:

http://www.eternallyconfuzzled.com/articles/rand.html