Random number generation, without built-in random number generation libraries [duplicate]

Make a random number generator. The catch is that you can't use any built-in randomness functions. Your random numbers must be anything between 0 and 100, though they do not have to be floating point.

Rules:

• It must produce a pseudorandom number.
• Randomness defined: If you run it x times, then x times again, the two sequences will be unlikely to be identical. Where 2 < x < 10.
• It is not required that your random number generator passes any specific test about the quality of the generated numbers, as long as the previous rule is respected.
• Any language is accepted, except Golfscript, see here.
• This is , so the shortest answer in bytes wins.
• You may not hard-code the output numbers or read them from a file.
• Post 5 numbers generated by your program.
• Good luck!

marked as duplicate by Howard, Fors, Peter Taylor, John Dvorak, Doorknob♦Mar 1 '14 at 22:02

• Because I was typing the moment you commented: -1 because of arbitrarily restricting the languages accepted. – Howard Feb 24 '14 at 19:39
• I would recommend you give an objective definition of "random". Is pseudorandom enough? Or does it have to be true random? In the latter case, how do you test it? (E.g. Diehard tests? en.wikipedia.org/wiki/Diehard_tests ) – ace Feb 24 '14 at 19:41
• Moreover it seems to be a duplicate (codegolf.stackexchange.com/q/10553/1490). – Howard Feb 24 '14 at 19:43
• @Howard - first off, see the link next to the language restriction, and second of all, that question does not restrict the use of built-in functions – TheDoctor Feb 24 '14 at 19:54
• -1 for forbidding golfscript, per my "tradition" of sorts of downvoting these sorts of restrictions. – Doorknob Feb 24 '14 at 22:00

C (46)

The stack pointer is sort of a random number

main(x){printf("%d",((unsigned char)&x)%100);}


Numbers I got:

20
40
4
52
36

• The question requires the numbers to be between 0 and 100. Also, please include the language and the character count in your answer. By the way, this can be golfed down a lot more. You can remove <stdio.h>, and remove the type definition of main. Here's a completely golfed down version: main(x){printf("%d",&x);} – ace Feb 24 '14 at 21:11
• Are these the rules of the game? I don't think the compiler will like that – Kirill Kulakov Feb 24 '14 at 21:27
• tested on gcc and it works perfectly, as long as you ignore the warnings (warnings are not errors, and the program is still valid). In C, if no type is specified, the compiler assumes int. Also, by defining x as an argument of main, you save yourself a semicolon. I'm just trying to help though, if you don't want to change, it's perfectly fine with me, but please add your language to your answer! – ace Feb 24 '14 at 21:33
• No parens around (unsigned char)&x needed, (typecast) binds tighter than % – mniip Feb 24 '14 at 22:10
• On my mac, this produces the same output with every run. – blabla999 Feb 25 '14 at 6:47

python, 49

x=7
def r(x):print(x%100);return(69069*x+1)%2**32


This is my first time golfing, I think this is within the rules but I'm not sure. This is a simple linear congruential generator, with starting x value as seed. User time is not used. To use, type x=r(x).

Here are the results:

7
84
25
62
47


Linux utils and ALSA, 41

Not a winner, but possibly a more accurate source of random noise than the current time:

arecord -qfS32|od -l -j48 -N8|cut -c27-28


Sample 5 random numbers:

32 53 81 83 26

• Unless you have no ALSA... – TheDoctor Feb 24 '14 at 22:26
• @TheDoctor If you have no ALSA, you better update your kernel to a non-ancient version – mniip Feb 25 '14 at 0:32
• Oh, i have ALSA – TheDoctor Feb 25 '14 at 0:45

Java - 250 249 characters

import java.awt.*;class A{public static void main(String[]y)throws Exception{int z=200,u=z*z-1,n=7;java.awt.image.BufferedImage b=new Robot().createScreenCapture(new Rectangle(z,z));while(u>0)n^=b.getRGB(u/z,u--%z);System.out.println((n>>>1)%101);}}


It uses the topright 200x200 pixels of the screen as a source of randomness. The result is an integer between 0 and 100.

I runned it 5 times (this last version) and got this:

97
84
38
40
65


I had to move my windows around between each run.

Needless to say, it is a very poor random-generator:

• It is frequently predictable.
• It is easily stalled to keep giving always the same output.
• It is unlikely to produce each number between 0 and 100 with an equal probability.

So, this should not be used by any serious purpose.

Java - 206 characters

class V{public static void main(String[]y)throws Exception{java.io.InputStream o=new java.net.URL("http://bit.ly/1gxstDI").openStream();int x,n=7;while((x=o.read())>0)n^=x;System.out.println((n>>>1)%101);}}


Similar to my previous answer. However, it is smaller and has some better randomness properties (but still bad). It fetches random data from http://en.wikipedia.org/wiki/Special:random as a seed (I am borrowing this idea from this answer).

Running it five times, here is the output:

25
15
93
32
9

• An argument could be made that you are using a built in of some other language is issuing. – Isiah Meadows Feb 25 '14 at 6:02

bash, 17

date +%N|tail -c3


Outputs the last two digits of a nanosecond timestamp.

Sample output

$./random 24$ ./random
52
$./random 82$ ./random
69
$./random 72  APL, 9 chars 101|+/⎕TS  Nothing new. It sums the components of the TimeStamp vector (current year, month… down to ms) and takes the remainder mod 101. Python 2.7 - 82 bytes import timeit print timeit.Timer('for i in range(1000):i**i').timeit(1)*10000%100  This can definitely be golfed down more. I couldn't get timeit working when using from timeit import* but I think I only lost one character there. This is my first time using the module so I'm probably missing a lot of stuff that could save characters. Times how long it takes in (seconds?) to calculate i**i for 0-999, multiplies that by 10000 and takes the modulus of 100 to get the result in the correct range. Since this works off of hardware deviation rather than a pseudorandom formula like most programs do, you could even call this "more random" than random.random()*100. :) (That last part probably isn't true, but until someone explains why I'll continue to believe it.) • Last part is not true at all on machines with single running process, or in environments especially made to emulate that situation. – yo' Feb 24 '14 at 19:54 • Dang, I felt clever for almost a whole minute. :P – undergroundmonorail Feb 24 '14 at 19:55 • In most situations, using the runtime tickcount is quite a safe seed (similar to what you do), but certainly not in the system's kernel, since the risk of reoccurance of the same seed is more than high. – yo' Feb 24 '14 at 19:59 Perl, 17 Seems like predictability isn't so much of an issue here, so here's a simple one: print time**3%101  Five outputs: 59 38 17 14 94  I hope this answer highlights the importance of objectively defining the question. • You could drop the **3 and shave 3 bytes off. It won't change the distribution or the predictability of your "RNG" – Tobia Feb 24 '14 at 21:56 • @Tobia I'm not really into the challenge, I just want to show by example that this question is really very poorly defined – ace Feb 24 '14 at 22:13 • Yes, well, depending on the circumstances, using the time of the day as a pseudo-random number can be perfectly acceptable. For example, to initialise Randomize() at the start of one's program. (Assuming an "attacker", for any definition of the term, cannot control the time of the day when your program is started or restarted.) – Tobia Feb 24 '14 at 22:18 • @Tobia I am aware of this usage. But at least those algorithms will be difficult to predict. – ace Feb 24 '14 at 22:26 PowerShell (66) [Convert]::ToInt32((""+([guid]::newguid())).SubString(0,2),16)%100  5 outputs: 10 92 13 84 0  Ruby, 69 def r()(Time.now.to_f.to_s+$$.to_s+-I.join).bytes.reduce(:+)%100;end  Online Version for testing. Uses the current time in ms, the process id ($$) and the runtime environment ($-I) for creating a pseudo-random number.

Output:

47
44
39
43
88

• Run it five times and post the output. – Victor Stafusa Feb 24 '14 at 22:03

Java, 99 chars

class b{public static void main(String[]a){System.out.println(System.nanoTime()/a.hashCode()%99);}}

75
28
34
88
16


If you want to be really really cheap:

java 88 Chars

class b{public static void main(String[]a){System.out.println(System.nanoTime()/9%99);}}

99
86
61
15
19

• Run each one five times and post the output. – Victor Stafusa Feb 24 '14 at 22:01

Smalltalk, 39

not a production quality random generator:

OperatingSystem getMicrosecondTime\\101


but as I generated those 5 values by eval'ing them interactively, it's me who makes it random.

Output of 5 evaluations:

 93 28 25 6 25


PS: the free running us timer is normally used for profiling...

A much better random gen would be:

'/dev/random' asFilename readStream nextByte/2.55

• The latter is only available on *nix systems, and is a built-in (not to Smalltalk, but the OS) – Isiah Meadows Feb 25 '14 at 6:04

bash — 108 106 bytes

ls -al|tee -a X|grep -o [0-9]|xargs -n3|sed 's/ //g'|sort -u|awk 'BEGIN{X=0}{X=xor(X,$0)}END{print X%101}'  Gets a set of all three-digit triplets from a detailed listing, exclusive ORs them with each other, then prints a result modulus 101 to get [0, 100]. Depending on the activity in the directory, may be random-ish, or dynamic but predictable, but should never be static. Running it 1000 times, I was surprised that there didn't seem to be any patterns. Sample output: $ ls -al|tee -a X|grep -o [0-9]|xargs -n3|sed 's/ //g'|sort -u|awk 'BEGIN{X=0}{X=xor(X,$0)}END{print X%101}' 37$ ls -al|tee -a X|grep -o [0-9]|xargs -n3|sed 's/ //g'|sort -u|awk 'BEGIN{X=0}{X=xor(X,$0)}END{print X%101}' 95$ ls -al|tee -a X|grep -o [0-9]|xargs -n3|sed 's/ //g'|sort -u|awk 'BEGIN{X=0}{X=xor(X,$0)}END{print X%101}' 62$ ls -al|tee -a X|grep -o [0-9]|xargs -n3|sed 's/ //g'|sort -u|awk 'BEGIN{X=0}{X=xor(X,$0)}END{print X%101}' 6$ ls -al|tee -a X|grep -o [0-9]|xargs -n3|sed 's/ //g'|sort -u|awk 'BEGIN{X=0}{X=xor(X,\$0)}END{print X%101}'
51

• The question asks for 5 generated numbers. So run it one more time. – Victor Stafusa Feb 24 '14 at 22:04
• @Victor - done. – Yimin Rong Feb 24 '14 at 22:26
• Use "sort -u" instead of "sort|uniq" to save 4 bytes – Glenn Randers-Pehrson Feb 24 '14 at 22:50
• @GlennRanders-Pehrson - Thank you! – Yimin Rong Feb 25 '14 at 14:30

CMD / Batch - 14 Bytes

echo %time:~9%


Grabs milliseconds off the end of the variable %time%.

Add @ before the command to make it only output the number.

C:\>echo %time:~9%
40

C:\>echo %time:~9%
53

C:\>echo %time:~9%
19

C:\>echo %time:~9%
73

C:\>echo %time:~9%
25

• Any output? Run it 5 times and post – TheDoctor Feb 26 '14 at 23:35
• @TheDoctor please see my updated answer. – unclemeat Feb 27 '14 at 0:11