Random.randrange() is really random?
- Thread starter ChrisVer
- Start date
-
- Tags
- Random
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 3K views
Discussion
Physics news on Phys.org
Mentor
- 38,146
- 10,752
There is the random module (Lib/random.py), which contains the Random class.ChrisVer said:I was wondering how could I check how random the random.randrange() method of python's random package (?or class?) is...
How could I do that by building a macro?
Maybe check the two hypothesis: is good random generator vs is not?
The documention (v3.4.2) contains this warning in its documentation of the random module:
Warning
The pseudo-random generators of this module should not be used for security purposes. Use https://www.physicsforums.com/os.html#os.urandom or SystemRandom if you require a cryptographically secure pseudo-random number generator.
Regarding randrange(), the docs say this:
I don't know what you mean by "building a macro".Changed in version 3.2: randrange() is more sophisticated about producing equally distributed values. Formerly it used a style like int(random()*n) which could produce slightly uneven distributions.
Last edited by a moderator:
Mentor
- 8,478
- 5,699
Have a look at this excerpt from the book Beautiful Testing to see how to test RNGs.
Science Advisor
- 3,372
- 465
Mark44 said:I don't know what you mean by "building a macro".
I am thinking something like the following:
Python:
import random
event = 0
number = 0
lista = []
occuranc = []
while event<10000:
x = random.randrange(0,100)
lista.append(x)
event += 1
while number<100:
y = lista.count(number)
occuranc.append(y)
number += 1
Here I used the randrange to generate 10000 numbers between 0 and 100 (saved in the list "lista"). In the second while I am counting the multiplicity of appearence of 0, 1, 2, ...,100 in those 10000 numbers (the multiplicity is saved into my "occuranc" list).
Normally if the generator produces the numbers from a uniform distribution the probability of occurrence for each number should be (1/100) and so each number should appear with multiplicity 10000/100=100.
So I am not sure how to continue the above code into making the chi-squared test of this hypothesis. In other words I would like to test by myself at what confidence level I can say that the randrange is a uniform-distribution generator. Like the test should check whether if you plot the occurrence vs number whether it will be described by a [itex]y=const[/itex] relation or anything else (uniform pdf or not).
Last edited:
Mentor
- 38,146
- 10,752
You have your list of frequencies. Calculate the sum of the squared differences between your frequencies and the theoretical value it should have (100). The process is laid out here - https://en.wikipedia.org/wiki/Pearson's_chi-squared_test - in the definition section. (Apologies if you already know this...)
If you need help implementing this process in Python, give a holler.
If you need help implementing this process in Python, give a holler.
Science Advisor
Homework Helper
- 9,572
- 4,799
I would be surprised if any of the standard random number generators would not pass a Chi-squared goodness of fit test.
That being said, none of the "pseudo-random" random number generators are truly random. There is always a test that is sophisticated enough to determine that they are not random. Unless you are going to apply statistical methods like Box-Jenkins time series analysis, the standard random number generators will probably work fine for you. But if you are going to use those methods, you should test your random number generator with them first. Verify that the generator will look random when those methods are applied.
That being said, none of the "pseudo-random" random number generators are truly random. There is always a test that is sophisticated enough to determine that they are not random. Unless you are going to apply statistical methods like Box-Jenkins time series analysis, the standard random number generators will probably work fine for you. But if you are going to use those methods, you should test your random number generator with them first. Verify that the generator will look random when those methods are applied.
Last edited:
Science Advisor
- 3,372
- 465
I think I made it... and passed the test by p-value ~0.26 with one test [the rest are almost the same]. So yes, it passed the test. I was a bit stuck with how to implement the code part for the chi2 determination in python because I didn't have my coffee on the desk (a good reasoning to avoid saying I was thinking dumb at that moment).FactChecker said:I would be surprised if any of the standard random number generators would not pass a Chi-squared goodness of fit test.
ha, quite intriguing... probably I will try to make that pseudorandom numb generator fail next.FactChecker said:There is always a test that is sophisticated enough to determine that they are not random.
Yup, that book contains the same test I am trying to apply, but what it actually tests there is whether given a uniform distribution it works fine to go to a non-uniform one (or so I understood) . It was interesting reading however, and almost the same method I did (the "bucket method" with 100 bins is the same I tried).DrClaude said:Have a look at this excerpt from the book Beautiful Testing to see how to test RNGs.
Science Advisor
Homework Helper
- 9,572
- 4,799
If your work involves a number generator and the work requires you to use more sensitive statistical tests, you should make sure that the number generator is good enough. I think problems would be rare. Most real-world applications of techniques like the Box-Jenkins time series analysis are on real data, not generated data. Of course, examples for a class might be artificially generated. That is where I encountered a problem once, long ago. I generated artificial data for classwork and the number generator contained bad autocorrelations.ChrisVer said:I will try to make that pseudorandom numb generator fail next.
Similar threads
Is the 'Randomness' of Evolution Really Random?
- ShayanJ
- · Replies 63 ·
- Biology and Medical
- Replies
- 63
Random Seed Choice for LAMMPS Molecular Dynamics Simulations
- person123
- · Replies 21 ·
- Programming and Computer Science
- Replies
- 21
Can globals in Godot randomly fail to update?
- Darkmisc
- · Replies 4 ·
- Programming and Computer Science
- Replies
- 4
Identifying and understanding random number algorithms
- schniefen
- · Replies 2 ·
- Programming and Computer Science
- Replies
- 2
Is the Decimal Expansion of an Irrational Number Truly Random?
- sysprog
- · Replies 25 ·
- Programming and Computer Science
- Replies
- 25
Generating Random Numbers with the Acceptance-Rejection Method
- Amrator
- · Replies 22 ·
- Programming and Computer Science
- Replies
- 22
Fortran Solving Magnetic Field Using Floating Random Walk
- gt24
- · Replies 6 ·
- Programming and Computer Science
- Replies
- 6
How to check if a list of numbers is random?
- ORF
- · Replies 10 ·
- Programming and Computer Science
- Replies
- 10
Undergrad Sampling theory and random sample
- fog37
- · Replies 5 ·
- Set Theory, Logic, Probability, Statistics
- Replies
- 5
Undergrad Statistical modeling and relationship between random variables
- fog37
- · Replies 7 ·
- Set Theory, Logic, Probability, Statistics
- Replies
- 7