What is the Solution for Creating a Uniform Random Number Generator in C?

In summary, the author is trying to generate a uniform random number and is having trouble doing so. The author also says that the problem may be with the random number generator itself, not with the C programming language.
  • #1
newton1
152
0
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=rand()%10;
if(j==a)y++;
printf("%d",a);
fprintf(fptr,"%d\n",y);
y=0;
}
}
fclose(fptr);
}

and how can i create a uniform ramdom number generator
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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
 
  • #4
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]++;
}
 
  • #5
Thank you very much...
i know my ramdon generator is bad~~
i will find on some place...:smile:
 
  • #6
This article uses a tiny amount of math but is very clearly written:

http://www.eternallyconfuzzled.com/articles/rand.html [Broken]
 
Last edited by a moderator:

1. What is a random number in C?

A random number in C is a number that is generated by a computer program using a predefined algorithm. The value of the number is not predictable and is considered to be "random" in the sense that it does not follow any particular pattern.

2. How can I generate a random number in C?

To generate a random number in C, you can use the rand() function from the stdlib.h library. This function returns a pseudo-random number between 0 and RAND_MAX, which is a constant defined in the library. You can also use the srand() function to set a seed for the random number generator.

3. What is the range of values for a random number in C?

The rand() function in C returns a pseudo-random number between 0 and RAND_MAX, which is usually a very large value. However, you can use the modulus operator (%) to generate numbers within a specific range. For example, rand() % 100 will generate a random number between 0 and 99.

4. Can I generate a random number in C that is not an integer?

Yes, you can generate a random number in C that is not an integer by using the rand() function and then dividing the result by a number. For example, rand() / 10.0 will generate a random number between 0 and 1 with decimals.

5. Are random numbers in C truly random?

No, the random numbers generated by a computer program are not truly random as they are generated using a predefined algorithm. However, they are considered to be random enough for most applications and can be used effectively for simulations, games, and other purposes.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
1
Views
895
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top