Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

I will award the prize to the shortest code that can generate me a truly random number. I do not want something like %RANDOM% in Windows Batch where I can pretty-much guess what's coming next.

EDIT: After reading @dmckee's response, I have changed the conditions. It should just be a code that produces good pseudo-randomness.

share|improve this question
1  
What do you mean by "truly random"? – David Carraher Nov 26 '12 at 23:42
2  
"Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin." --John Von Neumann. Simply can not be done without access to an external entropy source. Enter /dev/random or random.org as below. Now, good pseudo-randomness is of course a programmatic possibility, but you might want to edit if that is what you mean. – dmckee Nov 27 '12 at 1:33
4  
You might also want to go into detail about what you would consider good pseudo-randomness. Must values produced be uniformly distributed (for any modulo)? Must all seeds have the same periodicity? Is a there a minimum acceptable periodicity? – primo Nov 27 '12 at 8:56

5 Answers

c

int getRandomNumber () 
{
    return 4; //chose by fair dice roll.
              //guaranteed to be random.
}

Credit: http://xkcd.com/221/

share|improve this answer
1  
don't forget to golf it – ardnew Nov 27 '12 at 2:34
int r(){return 4;} :) – Dogbert Nov 27 '12 at 5:09
r(){return 4;} – ugoren Nov 27 '12 at 11:20
Some versions of MinGW will treat the last assignment as a return with -g... r(){int a=4;} but that's invoking undefined behavior – Aslai Nov 30 '12 at 2:35
@Aslai It's not strictly the last assignment, it's the last operation that used the eax register, since that's what most calling conventions use for storing the return value. MinGW may prefer eax for integer assignments, which would explain the better predictability. Regardless of the compiler, if you can trick it into using the return value of a method whose return type is implicitly void, you'll get whatever value was last placed in the register that the calling convention defines as the location for storing a return value. – Polynomial Dec 9 '12 at 23:37

HTTP, 91 23 chars

Generates a "truly random" number between 0 and 1000.

GET http://goo.gl/7dO8c
share|improve this answer
You beat me to it! No use in creating a new answer so here's some Python code (input on commandline in format [NUM, MIN, MAX]): import urllib, sys;print urllib.urlopen('http://www.random.org/integers/?num=%s&min=%s&max=%s&col=1&base=‌​10&format=plain&rnd=new'%tuple(sys.argv[1])).read() – beary605 Nov 27 '12 at 0:34
@beary605 Haha! Great minds... I almost wrapped it in code as well, but I thought the question would probably get closed. :) – Paul Walls Nov 27 '12 at 0:40
3  
You do know that there are websites devoted to golfing URLs? ;) – Peter Taylor Nov 27 '12 at 7:48
@PeterTaylor Ah yes... what was I thinking? :) – Paul Walls Nov 27 '12 at 8:06

Python, 8 characters

id('')%5

Generates a random number between 0 and 9 with limited entropy. Number doesn't change during run of the script - rerun the script for a new number.

share|improve this answer
In cPython id(0)%7 works as well (or any other modulus co-prime to 4). It's not exactly a good prng, though. In fact, consecutive executions will quite often result in the same value, if the Python interpreter was allotted the same slice of memory. – primo Dec 10 '12 at 6:51

Ruby, 6 Characters

p rand

Does rand count as truly random?

share|improve this answer

In Shell scripting $RANDOM is defined as an environment variable. So you can access it by

echo $RANDOM

If in case you want to produce a sequence of random numbers do

for i in [ 1..100 ]
do
echo $RANDOM
done
share|improve this answer
That [ 1..100 ] is the strangest list of values to make the for loop 3 times. Or maybe you intended to write {1..100} to make for loop 100 times? – manatwork Dec 26 '12 at 10:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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