Take the 2-minute tour ×
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
    
VTC as IO requirement not specified. –  user80551 Apr 14 at 14:28
add comment

closed as unclear what you're asking by user80551, ProgramFOX, Geobits, TimWolla, algorithmshark Apr 14 at 17:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

7 Answers

up vote 10 down vote accepted

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
add comment

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
show 1 more comment

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
add comment

Ruby, 6 Characters

p rand

Does rand count as truly random?

share|improve this answer
add comment

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
add comment

Based on the solution by @Paul Walls, but with a different URL shortener...

I haven't tested this.

HTTP, 21 chars

GET http://x.co/3srR5

Gets a random byte (0-255) formatted in base-10 from random.org.

share|improve this answer
add comment

Nobody said anything about the range or base of the numbers...

These are actually "truly random" as they're from random.org which fits the criteria.

You can save one character by removing the -q option, but this will make the program print a load of rubbish as well as the random number.

EDIT: removed http://www. at start of URLs.

EDIT: switched URL shortener to ow.ly x.co to save characters

Bash (with URL shortening), 30 28 27

wget x.co/3srR5 -qOr&&cat r

Does exactly what the following one does.

Bash, 75 71 64

wget "random.org/cgi-bin/randbyte?nbytes=1&format=d" -qOr&&cat r

Gets a random byte (0-255) formatted in base-10 from random.org, saves it to a file called r and prints the file's contents.

Bash, 94 90 83

wget "random.org/integers/?num=1&format=plain&base=1&col=1&max=1&min=0" -qOr&&cat r

Gets a random number from 0 to 1 (to save characters in the max and min sections of the URL), formats it in binary (to save characters in the base section of the URL), saves it to a file called r and prints the file's contents.

Also, just because I can...

Bash (with lynx, very cheaty), 15

lynx x.co/3srR5

Gets a random byte and displays it on-screen as a number.

share|improve this answer
add comment

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