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

Click For Summary

Discussion Overview

The discussion revolves around creating a uniform random number generator in C and assessing the uniformity of the generated numbers within a specified range. Participants explore issues related to the implementation of random number generation and the evaluation of its uniformity.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to generate random numbers in the range [0,9] and check for uniformity, but reports that their program is not functioning as expected.
  • Another participant suggests that the issue may stem from a lack of clarity in the participant's goals and recommends writing detailed instructions for the intended process.
  • A different participant points out a potential problem in the code where a variable is reset each time, which could affect the outcome, and suggests consulting literature on random number generators.
  • Another participant shares an alternative code snippet for generating random numbers and counting occurrences, indicating that there may be a better approach to the problem.
  • One participant acknowledges the inadequacy of their random generator and expresses intent to seek further information.
  • A participant provides a link to an article that discusses random number generation, noting its clarity and minimal mathematical complexity.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the specific issues with the original code or the best approach to creating a uniform random number generator. Multiple viewpoints and suggestions are presented without resolution.

Contextual Notes

There are indications of missing assumptions regarding the definition of uniformity and the specific requirements for the random number generator. The discussions also highlight potential misunderstandings in the implementation of the code.

newton1
Messages
151
Reaction score
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
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.
 
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
 
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]++;
}
 
Thank you very much...
i know my ramdon generator is bad~~
i will find on some place...:smile:
 
This article uses a tiny amount of math but is very clearly written:

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

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
22
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
11
Views
2K
Replies
19
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
2K