|
Simple test for randomness +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html) +--- Forum: HP Prime Software Library (https://www.hpmuseum.org/forum/forum-15.html) +--- Thread: Simple test for randomness (/thread-24203.html) |
Simple test for randomness - Namir - 2025-10-17 13:15 Here is a program that performs a simple test for randomness of a uniform PRNG function. The exported function RANDO takes the current random number and returns the next random number. The function RANDOSTATS generates random numbers and returns the following statistics: 1. The mean value for the random numbers generated. The ideal value is 1/2. 2. The standard deviation for the random numbers generated. The ideal value is 1/sqrt(12) (about 0.28868). 3. The chi-square statistics for the bins that contains the count of random numbers distributed in the bins. 4. The inverse Chi-square statistic used to compare with the calculated value in step 3. If the inverse Chi-square statistic is greater, the random number generated are accepted as sound. 5. A list of values for the bins containing the count of random numbers distributed in these bins. The function RANDOSTATS takes the following parameters: 1. The parameter seed is the initial seed for the random numbers and should be greater than 0 and less than 1. 2. The parameter n is the number of random numbers to generate. 3. The parameters nBins is the number of bins. The function converts your input into the smallest power of 10 value. For example the numbers 63, 111, and 923 translate into 10, 100 and 100, respectively. You can test your PRNGS by increasing the number of random values generated. If the calculate Chi-square remains less than the inverse Chi-square statistic, then your PRNG function is good. By contrast if you increase the number of random values generated an notice (a) the calculate Chi-square is always greater than the inverse Chi-square statistic, then your PRNG function performs very poorly, and (b) the calculate Chi-square gradually increases as the number of random values generated increases, switching from being less than the inverse Chi-square statistic to exceeding te value of the latter statistic, then your PRNG is weak is should be replaced. This observation happens because when the number of random values is relatively small, the problems of these values is overshadowed by the statistical noise. As the number of random values reach a critical level, the random values show their non-true randomness! Example 1 1. Enter RANDOSTATS(0.232233334446,10000,22)->L1 2. Enter L1 to get: {0.50121441209,0.290048772951,7.652,16.9189776046,[1001,995,1040,990,943,982,998,997,1048,1006]} Notice that the above command specifies 22 bins. The function translates that value into 10, which is the power of 10 that is less than 22. The mean and standard deviation values are close to their expected theoretical values. The Chi-square statistic of 7.652 is less than the inverse Chi-square statistic of 16.9189776046. Thus, the random numbers generated by the function RANDO are good. Example 2 1. Enter RANDOSTATS(0.436841235,10000,10)->L1 2. Enter L1 to get: {0.497295025,0.290667387192,11.756,16.9189776046,[1030,1012,1025,1017,972,941,1028,972,957,1046]} The mean and standard deviation values are close to their expected theoretical values. The Chi-square statistic of 11.756 is less than the inverse Chi-square statistic of 16.9189776046. Thus, the random numbers generated by the function RANDO are good. Here is the listing for the program: Code: EXPORT RANDO(X)Enjoy! Namir |