• yer mom (unregistered)

My two dice must be loaded! I get seven way more often than any other number :o

• Trevor (unregistered)

So in order to increase the randomness of a number, we independently randomize each digit of a 1-100 digit, decimal, 1-100 digit number?

I'd hate to see them come up with a number between 1 and 10 with this strategy. Especially since every number they obtain is going to be anywhere from 0 to 10^100.

• akatherder (cs)

Random() returns increasingly randomer numbers the more you call it.

• Bob (unregistered)

omfg

• mack (unregistered)

awful.....just plain awful...

• Erick (cs)

If I multiply a random by a random and then build a random string with it, then that must make it more random! What a revelation!

• Gabriel Kiss (unregistered) in reply to Erick
Erick:
If I multiply a random by a random and then build a random string with it, then that must make it more random! What a revelation!
yes it more random because you get a RANDOM^2 number!!!11one-eleven... and so on :)
• DaveK (cs) in reply to Trevor
Trevor:
So in order to increase the randomness of a number, we independently randomize each digit of a 1-100 digit, decimal, 1-100 digit number?

I'd hate to see them come up with a number between 1 and 10 with this strategy. Especially since every number they obtain is going to be anywhere from 0 to 10^100.

Well, since the range is so huge, I would have thought that just taking the result modulo ten should give a sufficiently even distribution; it's not like taking a 7-bit random from 0-127 modulo ten where each of the numbers 0-7 get 'one extra chance' to be picked than the others.

However looking at their code, I can only assume they'd generate a random number between 1 and 10 by calling the random routine and repeatedly looping until they got one that fell in range.....

... that could take a little while.

• Some Computer Scientist (unregistered)

Knuth devoted an entire section talking about random methods for generating random numbers. It boiled down to this: DON'T

Random numbers are serious business. In cryptography, a secure random number generator can mean the difference between impossible to crack and trivial to break.

This code is just an EPIC FAIL in every way.

• KattMan (cs)

I know I can get even more randomer.

Let rand return a number between 2 and 9. Multiply by 9. Add the digits of the result together. Subtract 5.

This should be as random as I need to get and as everyone knows, a formula is better than just calling rand().

• Brian S (unregistered)

The obvious solution would have been to set the random seed to a random value a random number of times before using Math.random to make it more randomer

• TraumaPony (unregistered) in reply to akatherder
akatherder:
Random() returns increasingly randomer numbers the more you call it.
Actually, depending on the algorithm, the more times you call it, the more predictable it becomes.
• Anon (unregistered)

To quote Knuth:

"Random numbers should not be generated with a method chosen at random."

Trevor:
I'd hate to see them come up with a number between 1 and 10 with this strategy. Especially since every number they obtain is going to be anywhere from 0 to 10^100.

Well I suppose they could do answer % 10 + 1, but I'm sure they have a much more convoluted function that does the same thing.

• gblues (cs)

Irish I were drunk too. Rowr.

• A Nonny Mouse (cs) in reply to Brian S
Brian S:
The obvious solution would have been to set the random seed to a random value a random number of times before using Math.random to make it more randomer

the more i read this sentence, the less "random" looks like a proper word..

• Colin (unregistered)

private int getRandomOverflowException() { return (int) ((Math.random() * 100) * Math.random()); }

• MAV (unregistered)

Wonder what they're doing that needs super-extra-ultra random numbers?

• MAV (unregistered)

just remember. random * random = ultra-random

• RobIII (unregistered) in reply to TraumaPony
TraumaPony:
akatherder:
Random() returns increasingly randomer numbers the more you call it.
Actually, depending on the algorithm, the more times you call it, the more predictable it becomes.

You have GOT to be kidding me! Where's your sarcasm detector?

DUDE...

• John the incredible (unregistered)

Prime numbers are too predictable... Let's make sure we get less of those.

• Geezer coder (unregistered) in reply to MAV

"Wonder what they're doing that needs super-extra-ultra random numbers? "

They are getting paid by the line of code developed.

• Patrick (unregistered)
Comment held for moderation.
• N (unregistered) in reply to Geezer coder

I get that impression with a lot of the WTFs presented here.

• Recycled contractor (unregistered)

Why not just return '1' every time?

After all, it's theoretically possible that your perfectly-random die rolls repeated 1's. And your callers wouldn't know any different, unless they kept a history (which most don't)

;-)

• Xandax (cs)

Still use Dilberts Random Number Generator:

Nine, Nine, Nine, Nine, Nine, Nine, Nine, Nine.

• bramster (unregistered)

I like to sort my random numbers.

• Nerf Herder (unregistered) in reply to KattMan
KattMan:
I know I can get even more randomer.

Let rand return a number between 2 and 9. Multiply by 9. Add the digits of the result together. Subtract 5.

This should be as random as I need to get and as everyone knows, a formula is better than just calling rand().

OMGZ that totally equals my birthday! It really DOES work!

Now forward this to 20 friends or u'll have bad luck. LOLZ.

• APH (unregistered) in reply to gblues
gblues:
Irish I were drunk too. Rowr.
Irish she were drunk too. Rowr.

There, fixed that for ya.

• my name is missing (unregistered)

perhaps it was written by a random code generator

• grg (unregistered)

actachally, taking n sequential samples from a random number generator, you may end up with a less-random number.

• Raedwald (cs)

A little bit of knowledge is a dangerous thing.

• Brandon (unregistered)

they should call those guys HD Random Numbers...for "High Degree" of Randomness. The degree increases as you multiply by more random numbers!

• Grammar Nazi (unregistered)

Apostrophe's and they're use's.

• Richard (unregistered) in reply to Grammar Nazi

int getRandomNumber() { int ret; printf("Please throw the dice that you will find under your chair and enter the result"); scanf("%d",&ret); return ret; }

• yer mom (unregistered)
```/**
* We all know that random number generators aren't truly random.
* This function generates random numbers a bit more randomly by using a hardware source.
*/
int random() {
FILE* fp;
char count;
float final = 0;
char i;

fp = fopen("/dev/null", "r"); /* open hardware source */
count = fgetc(fp); /* more randomly */
for (i = 0; i < count; i++) {
final *= ((float)fgetc(fp)) / 255.0;
};
};```
• kw (unregistered)

So, there is a 1 in 100 chance that the value left of the '.' is 0, and there is a 1 in 100 chance that the value left of the '.' will range between 1*10^100 to 9.999...*10^10 ?

Does true randomness typically lean towards smaller values?

• Random832 (cs)
```int getRandomNumber() {
return 4; // chosen by fair dice roll, guaranteed to be random.
}
```
• me (unregistered)

I particularly like it how standard generators are not good enough for this "programmer" (despite using them...), but it's OK to have getRandomDigit() heavily biased against returning '9' :-)

• RobertB (cs)

The most random thing about the code is that it's nicely obfuscated. But I think this is what's going on -- forgive me for being obvious, but I really want to know what it does so I never, EVER do it myself.

• getRandomDigit does a pretty reliable job of returning one of the ten strings '0' through '9'... I think.

• getRandomNumberOfDigits generates a number between 0 and 100, then multiplies it by a number between 0 and 1, generating a significantly less random number between 0 and 100. It converts it to an integer, so the number will probably be between 0 and 99. Converting to integer already does a pretty good job of de-randomizing the number, so the multiplication really just wastes clock cycles.

• getRandomDigitString calls getRandomNumberOfDigits to see how many numbers should be in the string. However, getRandomNumberOfDigits could return zero. So it discards 1% of its randomness and equates "zero digits" to "one digit", with that digit being "0". Maybe it only eliminated 0.1% of the randomness.

• Finally, take a nearly-random string of 1-99 numbers (with a greater than 1% chance that the string is "0"), add a decimal, add another nearly-random string of 1-99 numbers (again, good odds that the string is "0"), and cram it into a Double.

This is about as good a WTF as you can get. The function works without error, doing exactly what it was programmed to do, but the world would be a better place if it would just throw a "YOU CAN'T BE SERIOUS" error and die.

• KT (unregistered) in reply to Richard
Richard:
int getRandomNumber() { int ret; printf("Please throw the dice that you will find under your chair and enter the result"); scanf("%d",&ret); return ret; }

fprintf(stderr, "DICE_NOT_FOUND");

• IV (unregistered) in reply to TraumaPony

I think a name change for the site is in order. I suggest the Daily What In Tarnation (WIT). Maybe if "wit" were explicitly in the title people would be more inclined to recognize it.

• Maybe Anon (unregistered) in reply to me

It's also final so that people can't subclass it and change his logic...

This is a ordered version of 20 runs, note how it goes from very very small, to very very big, very very quickly. Very very.

The numbers look about the same with thousands of runs, very small then very big. I saw a few plain "0"s too.

```0.5215661175298943
4.134056449938214
6.0
87.0
5785.430245466055
7595.48417053833
110939.37712878542
488733.77795
5.352956622495464E10
9.462390630556685E10
1.2070032004517574E13
5.2092107513286E13
9.157946886121803E13
2.9497107991280284E25
1.834136407474311E32
1.7446213245558947E41
8.45034288762292E43
6.632080987216593E58
8.500084477175984E67
2.1875718001884665E72
```
• sweavo (unregistered) in reply to Maybe Anon

There ya go, very small and very big. How random do you want?

It's interesting to note though, that not only do the numbers themselves vary, but so does their randomness.

• sweavo (unregistered)

I asked my brian to plot a distribution curve of this, and it asplode.

Assuming math.random is evenly distributed between 0.0 <= x < 1.0

then number of digits is going to peak around 25 in't it?

so then your distribution of actual values is some freaky integral of an exponential.

• snoofle (cs) in reply to RobertB
RobertB:
The most random thing about the code is that it's nicely obfuscated. But I think this is what's going on -- forgive me for being obvious, but I really want to know what it does so I never, EVER do it myself.
• getRandomDigit does a pretty reliable job of returning one of the ten strings '0' through '9'... I think.

• getRandomNumberOfDigits generates a number between 0 and 100, then multiplies it by a number between 0 and 1, generating a significantly less random number between 0 and 100. It converts it to an integer, so the number will probably be between 0 and 99. Converting to integer already does a pretty good job of de-randomizing the number, so the multiplication really just wastes clock cycles.

• getRandomDigitString calls getRandomNumberOfDigits to see how many numbers should be in the string. However, getRandomNumberOfDigits could return zero. So it discards 1% of its randomness and equates "zero digits" to "one digit", with that digit being "0". Maybe it only eliminated 0.1% of the randomness.

• Finally, take a nearly-random string of 1-99 numbers (with a greater than 1% chance that the string is "0"), add a decimal, add another nearly-random string of 1-99 numbers (again, good odds that the string is "0"), and cram it into a Double.

This is about as good a WTF as you can get. The function works without error, doing exactly what it was programmed to do, but the world would be a better place if it would just throw a "YOU CAN'T BE SERIOUS" error and die.

Hi there - OP here. I can't believe nobody mentioned that no routine that depends upon a random number generator can ever really be any more random than the underlying generator being used. In other words, assuming that the routine is implemented perfectly (as many have pointed out, it's not), it can never be any more random than: Math.random()

• Derek (unregistered) in reply to KattMan
KattMan:
I know I can get even more randomer.

Let rand return a number between 2 and 9. Multiply by 9. Add the digits of the result together. Subtract 5.

This should be as random as I need to get and as everyone knows, a formula is better than just calling rand().

that always equals 4. Why not just use xkcd's version, it's less wordy, but has the same result.

• tchize (cs) in reply to Richard
Richard:
int getRandomNumber() { int ret; printf("Please throw the dice that you will find under your chair and enter the result"); scanf("%d",&ret); return ret; }

Hey, I DID that. Didn't know random number existed, was using pascal, was years ago, one of my very first programs. You feel ashame when you discover there exist a random() function :/

At least it wasn't professional :D

• Nerf Herder (unregistered) in reply to Derek
Derek:
KattMan:
I know I can get even more randomer.

Let rand return a number between 2 and 9. Multiply by 9. Add the digits of the result together. Subtract 5.

This should be as random as I need to get and as everyone knows, a formula is better than just calling rand().

that always equals 4. Why not just use xkcd's version, it's less wordy, but has the same result.

I think it was sarcastic.

• Felix (unregistered)

This is what I would do:

``````int counter = -1;
public int getRandomInt() {
counter++;
switch (counter) {
case 0:
return 4;
case 1:
return 2;
case 2:
return 5;
case 3:
return 4;
//and so on
default:
//this can't happen anyway....
throw new SomeException();
}
}
``````

You just need to do a proper sizing on project start and some cheap labor to roll dice. You could even sell updates to your software supplying new randomness!