patterncsharpModerate
Proof that I was wrong about Random Number Generators
Viewed 0 times
randomnumberthataboutwrongwasgeneratorsproof
Problem
I wrote this code to show how wrong I was about Random Number Generators.
Context:
From Comments to this answer to Regularity in the "Rusty Towel of Mutual understanding"
that isn't really a percentage of chance though, the chance that the
number returned is under 10 and/or under 50 is not 10% or 50%. you
actually will have a higher percentage of numbers in the middle of the
range than at the extremes. and I wish I could reference something
here, but I don't remember why/how I know that. – Malachi 3 hours ago
@Malachi - you are wrong with that statement. – rolfl♦ 3 hours ago
I don't have access to running Java Code, but I could create a random number generator with C# and see what happens.... – Malachi 3 hours ago
@Malachi and you'd be testing the effect of using C#'s RNG with java calls. Java's Random.nextInt(int n), returns 0-(n-1), pseudorandomly distributed. – Pimgd 2 hours ago
I agree it would be totally different, but aren't both RNG's based on a C function or a C++ function? wouldn't they be fairly similar? – Malachi 2 hours ago
Is there anything that I can do to make it more apparent how wrong that I am about the distribution of random numbers? Anything I can do to make this code cleaner?
Output:
`How many numbers do you require?
5000
1: 57 --> Percentage: 1.1400 %
2: 43 --> Percentage: 0.8600 %
3: 55 --> Percentage: 1.1000 %
4: 39 --> Percentage: 0.7800 %
5: 57 --> Percentage: 1.1400 %
6: 61 --> Percentage: 1.2200 %
7: 44 --> Percentage: 0.8800 %
8: 52 --> Percentage: 1.0400 %
9: 52 --> Percent
Context:
From Comments to this answer to Regularity in the "Rusty Towel of Mutual understanding"
that isn't really a percentage of chance though, the chance that the
number returned is under 10 and/or under 50 is not 10% or 50%. you
actually will have a higher percentage of numbers in the middle of the
range than at the extremes. and I wish I could reference something
here, but I don't remember why/how I know that. – Malachi 3 hours ago
@Malachi - you are wrong with that statement. – rolfl♦ 3 hours ago
I don't have access to running Java Code, but I could create a random number generator with C# and see what happens.... – Malachi 3 hours ago
@Malachi and you'd be testing the effect of using C#'s RNG with java calls. Java's Random.nextInt(int n), returns 0-(n-1), pseudorandomly distributed. – Pimgd 2 hours ago
I agree it would be totally different, but aren't both RNG's based on a C function or a C++ function? wouldn't they be fairly similar? – Malachi 2 hours ago
Is there anything that I can do to make it more apparent how wrong that I am about the distribution of random numbers? Anything I can do to make this code cleaner?
Random rng = new Random();
Dictionary tallyCount = new Dictionary();
//Key is 1-100 and value is number of times it appears
for (int i = 1; i kvp in tallyCount)
{
double percentageOfTotal = (double)kvp.Value / (double)totalNumbers;
Console.WriteLine("{0}: {1} --> Percentage: {2}", kvp.Key.ToString(), kvp.Value.ToString(), percentageOfTotal.ToString("P4"));
}
Console.ReadLine();Output:
`How many numbers do you require?
5000
1: 57 --> Percentage: 1.1400 %
2: 43 --> Percentage: 0.8600 %
3: 55 --> Percentage: 1.1000 %
4: 39 --> Percentage: 0.7800 %
5: 57 --> Percentage: 1.1400 %
6: 61 --> Percentage: 1.2200 %
7: 44 --> Percentage: 0.8800 %
8: 52 --> Percentage: 1.0400 %
9: 52 --> Percent
Solution
You've implemented a program to produce a histogram (though in tabular rather than visual form). That lets you judge "by eyeball" whether the distribution looks uniform. You would expect each bin to contain 1% of the samples. But how much deviation is allowable before you lose confidence?
Since you used the word "proof" in your question, I feel compelled to mention that eyeballing would not be good enough proof in academic terms. Statisticians actually have quantitative tests (Pearson's Chi-Squared test) to answer these questions. (Such tests are frequently used in medical publications, for example.)
The hypothesis is: "These outputs of
That is Pearson's Chi-squared test. While it would be tricky to actually derive the table and thus automate the entire test, you could at least compute \$\chi^2\$, which is easy to do.
Since you used the word "proof" in your question, I feel compelled to mention that eyeballing would not be good enough proof in academic terms. Statisticians actually have quantitative tests (Pearson's Chi-Squared test) to answer these questions. (Such tests are frequently used in medical publications, for example.)
The hypothesis is: "These outputs of
rng.Next(1, 101) came from a discrete uniform distribution." You would start by calculating \$\chi^2\$. Then, you look up the \$\chi^2\$ value in the table for totalNumbers - 1 degrees of freedom. That gives you the probability that you have a uniformly distributed number generator.That is Pearson's Chi-squared test. While it would be tricky to actually derive the table and thus automate the entire test, you could at least compute \$\chi^2\$, which is easy to do.
Context
StackExchange Code Review Q#64430, answer score: 16
Revisions (0)
No revisions yet.