Talk:Random number generation

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Statistics (Rated Start-class, Low-importance)
WikiProject icon

This article is within the scope of the WikiProject Statistics, a collaborative effort to improve the coverage of statistics on Wikipedia. If you would like to participate, please visit the project page or join the discussion.

Start-Class article Start  This article has been rated as Start-Class on the quality scale.
 Low  This article has been rated as Low-importance on the importance scale.

Randomness is an observed entity[edit]

The article starts out with: "A random number generator (RNG) is a computational or physical device designed to generate a sequence of numbers or symbols that lack any pattern, i.e. appear random." But it should be the other way around: If an Observer find that a sequence lack any pattern, it appears random to him. Different observers may rate the same sequence differently. The randomness is not in the sequence.

Bo Domstedt

New software-based TRNG[edit]

I get the impression from various sources on the internet that random number generators (especially of the true variety) are suposedly very hard to create. I have however managed to create a Perl script which I believe to be a pretty much bullet-proof TRNG. I would like there to be a mention of it on Wikipedia but I know very little about the protocols regarding adding an entry to Wikipedia. Please can an expert in both Wikipedia and Perl thoroughly examine my script and if you think it is worth including, please can you put it in an appropriate place and add any relevant links to it from other Wikipedia pages. Thank you very much. Here is the script:

sub TRNGrand
 ($range) = @_;

#This Perl subroutine almost instantly outputs a true random #number between zero and $range inclusive to 15 decimal places. # #If you are considering purchasing a hardware TRNG, save your #money and use this script instead. # #The author has only tested this subroutine on Windows and #therefore does not know if it works on Unix. # #It works by counting the number of for-loop cycles per eight #microseconds several times and dividing the counts into two #groups. A bit (0 or 1) is then determined by which group wins. #A draw means "try again". This process is repeated for each bit. # #The very first count is not included in the groups because it #tends to be somewhat different to the other counts. # #The two groups are intermeshed according to the following pattern: # #ABABBABAABABBABABABAABABBABAABAB # #The pattern is irregular in order to guard against any regularities #in the computer. # #Tests confirm that it produces results which are fairly evenly #distributed and because of the way it operates, the results are #theoretically independent of each other. # #Unfortunately, the Perl "bignum" module and associated modules #break this subroutine so if you are writing a Perl script which #uses these modules, the author suggests that you use this #subroutine to populate an array with all of the random data your #script will need before issuing the "use bignum;" command.

use Time::HiRes qw(gettimeofday);

$decimalplace = 1; until ($decimalplace == 15) { $bitstring = ""; for ($binaryplace = 0; $binaryplace <= 3; $binaryplace++) { @loopcount = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); $loopcountsgroupA = 0; $loopcountsgroupB = 0; until ($loopcountsgroupA ne $loopcountsgroupB) { for ($runnumber = 0; $runnumber <= 32; $runnumber++) { $x8microsecondschanges = 0; $previousx8microseconds = "null"; until ($x8microsecondschanges == 2) { ($seconds,$microseconds) = gettimeofday; $x8microseconds = int($microseconds / 8); if($previousx8microseconds ne "null" && $x8microseconds ne $previousx8microseconds) { $x8microsecondschanges++; } $previousx8microseconds = $x8microseconds; if($x8microsecondschanges == 1) {$loopcount[$runnumber]++;} } } $loopcountsgroupA = $loopcount[1]+$loopcount[3]+$loopcount[6]+$loopcount[8]+ $loopcount[9]+$loopcount[11]+$loopcount[14]+$loopcount[16]+ $loopcount[18]+$loopcount[20]+$loopcount[21]+$loopcount[23]+ $loopcount[26]+$loopcount[28]+$loopcount[29]+$loopcount[31]; $loopcountsgroupB = $loopcount[2]+$loopcount[4]+$loopcount[5]+$loopcount[7]+ $loopcount[10]+$loopcount[12]+$loopcount[13]+$loopcount[15]+ $loopcount[17]+$loopcount[19]+$loopcount[22]+$loopcount[24]+ $loopcount[25]+$loopcount[27]+$loopcount[30]+$loopcount[32]; } if($loopcountsgroupA < $loopcountsgroupB) {$bit = 0;} else {$bit = 1;} $bitstring = "$bitstring$bit"; } $decimaldigit = unpack("N",pack("B32",substr("0"x32 .$bitstring,-32))); if($decimaldigit < 10) { $decimaldigits = "$decimaldigits$decimaldigit"; $decimalplace++; } } if($range eq "") {$range = 1;} return "0.$decimaldigits" * $range; } (talk) 18:20, 29 January 2014 (UTC)

Wikipedia is not the place to publish original research (see WP:OR). There are many Category:Cryptography journals for that. I would call your attention to the fact that the problem you are addressing has numerous pitfalls. In particular, not all operating systems (e.g. Windows) provide time of day to microsecond resolution. --agr (talk) 19:23, 29 January 2014 (UTC)

Quote: "In particular, not all operating systems (e.g. Windows) provide time of day to microsecond resolution." Windows does support it. Otherwise my script would not work. I have successfully tested it on Windows. (talk) 19:35, 29 January 2014 (UTC)

Maybe so, but this is still not the place to publish your idea. Good luck with it, tho.--00:00, 30 January 2014 (UTC)
To Windows does not keep time to microsecond resolution. It will return a time value wherein one unit represents a tenth of a microsecond, but that value is only incremented every half a millisecond, at the most - default is every 1/64 of a second. You may be thinking of the CPU cycle counter ("time stamp counter"), but that doesn't "keep time"; its value is not correlated with wall clock time. Jeh (talk) 23:48, 31 December 2015 (UTC)

Poor example MWC1616 should be replaced[edit] claims that this article uses the poor example MWC1616. It would be nice if someone more qualified would check the link and article, and possibly insert a more appropriate example of a PRNG.

If the current example for chosen simplicity and briefness rather than quality, it might be reasonable to instead pick something even simpler like DJB2, which I believe is a lot easier to explain. If instead quality is an important factor, MWC1616 seems to be a poor choice even in the refined variant, according to the link. --mafutrct (talk) 15:12, 21 November 2015 (UTC)

@Mafutrct: the stock standard PRNG algorithm is (as the linked article points out) the Mersenne Twister, but if you want something with a very short pseudocode and comparable quality, xorshift is just a few lines. Chrome will apparently use xorshitf128+ to replace MWC1616. --Tgr (talk) 23:41, 31 December 2015 (UTC)

Just as an update: MWC has since been removed from the article. Sadly without a replacement. --mafutrct (talk) 13:56, 18 May 2016 (UTC)


I've always been led to believe that the digits of pi are truly random. Is this so? If so, worth a mention, I would think, since pi provides . . . how many now, 1 or 2 quadrillion random digits (I don't know what the current total number of known digits of pi is right now)? (Double that since presumably a random sequence is just as random when taken backwards(?)) DoctorFun1970 (talk) 15:03, 1 September 2016 (UTC)

The problem would be making a random number generator to provide a choice for which number in the digits of pi to use. WikipediaUserCalledChris (talk) 20:28, 23 January 2017 (UTC)