1
\$\begingroup\$

Task:

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!
\$\endgroup\$
25
  • 4
    \$\begingroup\$ Because I was typing the moment you commented: -1 because of arbitrarily restricting the languages accepted. \$\endgroup\$
    – Howard
    Commented Feb 24, 2014 at 19:39
  • 4
    \$\begingroup\$ 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 ) \$\endgroup\$
    – user12205
    Commented Feb 24, 2014 at 19:41
  • 3
    \$\begingroup\$ Moreover it seems to be a duplicate (codegolf.stackexchange.com/q/10553/1490). \$\endgroup\$
    – Howard
    Commented Feb 24, 2014 at 19:43
  • 2
    \$\begingroup\$ @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 \$\endgroup\$
    – TheDoctor
    Commented Feb 24, 2014 at 19:54
  • 2
    \$\begingroup\$ -1 for forbidding golfscript, per my "tradition" of sorts of downvoting these sorts of restrictions. \$\endgroup\$
    – Doorknob
    Commented Feb 24, 2014 at 22:00

15 Answers 15

5
\$\begingroup\$

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
\$\endgroup\$
8
  • 2
    \$\begingroup\$ 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);} \$\endgroup\$
    – user12205
    Commented Feb 24, 2014 at 21:11
  • \$\begingroup\$ Are these the rules of the game? I don't think the compiler will like that \$\endgroup\$ Commented Feb 24, 2014 at 21:27
  • \$\begingroup\$ 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! \$\endgroup\$
    – user12205
    Commented Feb 24, 2014 at 21:33
  • 1
    \$\begingroup\$ No parens around (unsigned char)&x needed, (typecast) binds tighter than % \$\endgroup\$
    – mniip
    Commented Feb 24, 2014 at 22:10
  • \$\begingroup\$ On my mac, this produces the same output with every run. \$\endgroup\$
    – blabla999
    Commented Feb 25, 2014 at 6:47
4
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ Unless you have no ALSA... \$\endgroup\$
    – TheDoctor
    Commented Feb 24, 2014 at 22:26
  • 4
    \$\begingroup\$ @TheDoctor If you have no ALSA, you better update your kernel to a non-ancient version \$\endgroup\$
    – mniip
    Commented Feb 25, 2014 at 0:32
  • \$\begingroup\$ Oh, i have ALSA \$\endgroup\$
    – TheDoctor
    Commented Feb 25, 2014 at 0:45
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ An argument could be made that you are using a built in of some other language is issuing. \$\endgroup\$
    – Claudia
    Commented Feb 25, 2014 at 6:02
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.)

\$\endgroup\$
3
  • \$\begingroup\$ Last part is not true at all on machines with single running process, or in environments especially made to emulate that situation. \$\endgroup\$
    – yo'
    Commented Feb 24, 2014 at 19:54
  • \$\begingroup\$ Dang, I felt clever for almost a whole minute. :P \$\endgroup\$ Commented Feb 24, 2014 at 19:55
  • \$\begingroup\$ 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. \$\endgroup\$
    – yo'
    Commented Feb 24, 2014 at 19:59
0
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ You could drop the **3 and shave 3 bytes off. It won't change the distribution or the predictability of your "RNG" \$\endgroup\$
    – Tobia
    Commented Feb 24, 2014 at 21:56
  • \$\begingroup\$ @Tobia I'm not really into the challenge, I just want to show by example that this question is really very poorly defined \$\endgroup\$
    – user12205
    Commented Feb 24, 2014 at 22:13
  • \$\begingroup\$ 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.) \$\endgroup\$
    – Tobia
    Commented Feb 24, 2014 at 22:18
  • \$\begingroup\$ @Tobia I am aware of this usage. But at least those algorithms will be difficult to predict. \$\endgroup\$
    – user12205
    Commented Feb 24, 2014 at 22:26
0
\$\begingroup\$

PowerShell (66)

[Convert]::ToInt32((""+([guid]::newguid())).SubString(0,2),16)%100

5 outputs:

10
92
13
84
0
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Run it five times and post the output. \$\endgroup\$ Commented Feb 24, 2014 at 22:03
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Run each one five times and post the output. \$\endgroup\$ Commented Feb 24, 2014 at 22:01
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ The latter is only available on *nix systems, and is a built-in (not to Smalltalk, but the OS) \$\endgroup\$
    – Claudia
    Commented Feb 25, 2014 at 6:04
0
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ The question asks for 5 generated numbers. So run it one more time. \$\endgroup\$ Commented Feb 24, 2014 at 22:04
  • \$\begingroup\$ @Victor - done. \$\endgroup\$
    – user15259
    Commented Feb 24, 2014 at 22:26
  • \$\begingroup\$ Use "sort -u" instead of "sort|uniq" to save 4 bytes \$\endgroup\$ Commented Feb 24, 2014 at 22:50
  • \$\begingroup\$ @GlennRanders-Pehrson - Thank you! \$\endgroup\$
    – user15259
    Commented Feb 25, 2014 at 14:30
0
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Any output? Run it 5 times and post \$\endgroup\$
    – TheDoctor
    Commented Feb 26, 2014 at 23:35
  • \$\begingroup\$ @TheDoctor please see my updated answer. \$\endgroup\$
    – unclemeat
    Commented Feb 27, 2014 at 0:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.