Comment On More Randomer

John B. saw some strange code that prompted him to investigate. [expand full text]
« PrevPage 1 | Page 2Next »

Re: More Randomer

2008-06-02 08:25 • by snoofle
Too...much... pain... this... early... on... Monday...

Re: More Randomer

2008-06-02 08:28 • by tag (unregistered)
http://xkcd.com/221/

Re: More Randomer

2008-06-02 08:28 • by St. Doodle (unregistered)
What happened to KISS?
Is he paid per line of code?
I shall headdesk now.

Re: More Randomer

2008-06-02 08:30 • by Eric (unregistered)
13 is not random enough. Only 13! Should have done 26 then it would be even more random... oh hold on...

Re: More Randomer

2008-06-02 08:31 • by XioPod
thirteenth!!!1!!11

Re: More Randomer

2008-06-02 08:32 • by floorpie (unregistered)
The WTF is deep in this one...

Re: More Randomer

2008-06-02 08:37 • by Alex G. (unregistered)
What kills me is... why is this thing Unix style paths? With the tilde as shorthand for home?

Re: More Randomer

2008-06-02 08:38 • by [ICR] (unregistered)
But it will choose between 6 header 1's and 7 header 2's....oh never mind.

Re: More Randomer

2008-06-02 08:38 • by StarLite
198292 in reply to 198290
Alex G.:
What kills me is... why is this thing Unix style paths? With the tilde as shorthand for home?

Thats Microsofts try at stability.

Re: More Randomer

2008-06-02 08:42 • by coward (unregistered)
Creating and seeding the Random generator every time you use it? Makes me wonder why those clever API programmers bother with the Next method... it's quite obviously redundant.

Re: More Randomer

2008-06-02 08:45 • by alex (unregistered)
And he reinitializes the random number generator every time !

Re: More Randomer

2008-06-02 08:47 • by Adam (unregistered)
What kills me is... why is this thing Unix style paths? With the tilde as shorthand for home?

In ASP.NET the tilde represents the application root.

Re: More Randomer

2008-06-02 08:48 • by Stooshie (unregistered)
Well, I'll say one thing anyway. It's possibly less predictable (especially if the rest of the code is done the same way).

Oh wait, unpredictable code... Oh yeh, maybe not so good after all!

Re: More Randomer

2008-06-02 08:51 • by NeoMojo (unregistered)
I read the code. I read it again. My brain started to melt.

Now that I am sufficiently stupified (or stupidified if you prefer), I feel compelled to justify the code, despite the OP's explanation, which negates a need for further rumination.

The original coder had to make it so that header2.jpg showed up 1/6th times more often than header1.jpg. It was in the specification. The customer is always right.

Re: More Randomer

2008-06-02 08:52 • by fruey (unregistered)
198299 in reply to 198290
The image path could have a different domain prefixed, e.g. they're using *nix/BSD for images, and IIS for random numbers (hey, maybe Microsoft is more random than *nix/BSD).

Imagine, they create an account sysimages on a *nix/BSD box to run images from... and maybe create other accounts for other images webimages, intranetimages, etc. and have a separate FTP access / password for each directory :-)

Re: More Randomer

2008-06-02 08:53 • by xg7b3

Re: More Randomer

2008-06-02 08:57 • by Proko (unregistered)
Well, I know a guy who tried to explain that using:
(random(max_number)*random(max_number)) mod max_number
is much more random usage then simply random(max_number). He didn't even care when one other guy proved mathematicly, that he was totally wrong.

Re: More Randomer

2008-06-02 08:57 • by bene (unregistered)
The only thing truly random in that code is the developer's thoughts at the time of witting it...

Re: More Randomer

2008-06-02 09:01 • by Crash Magnet (unregistered)
The guy takes an extra step to get better random selections, then chooses the first header 6 times, and the second header 7 times; thus blowing the whole random thing.

Why did he skip case 0?

Re: More Randomer

2008-06-02 09:06 • by ARG (unregistered)
198306 in reply to 198287
13 is not random enough. Only 13! Should have done 26 then it would be even more random... oh hold on...


Don't be silly. 26 isn't a prime!

Re: More Randomer

2008-06-02 09:06 • by snoofle
Maybe we should get the person who wrote the subject of today's post together with the person who wrote this piece of beauty.

Re: More Randomer

2008-06-02 09:09 • by Proko (unregistered)
198311 in reply to 198298
NeoMojo:
The original coder had to make it so that header2.jpg showed up 1/6th times more often than header1.jpg. It was in the specification. The customer is always right.

Please, for love of god, tell me you are kidding. It's of course interesting possibility to always great new object and not to use static one and in same time resolve( let's think, that you were right :) "1/6th times more often" problem with 13 case clauses, not with one if-else clause.
Ohh... and by the way, it is 1/13th not 1/6. Possiblity for header.jpg is 6/13 and for header2.jpg is 7/13.

Re: More Randomer

2008-06-02 09:10 • by jtl (unregistered)
See, my randomizer goes to 11.

Re: More Randomer

2008-06-02 09:24 • by NeoMojo (unregistered)
198318 in reply to 198311
Proko:
Please, for love of god, tell me you are kidding.
No. That is an excersise for you to determine for yourself.
Proko:
Ohh... and by the way, it is 1/13th not 1/6. Possiblity for header.jpg is 6/13 and for header2.jpg is 7/13.
so, if you got the perfect distribution of results for 13 attempts, i.e. 7 header2.jpgs and 6 header.jpgs, then then you would have 1/6th more header2.jpgs when compared to the number of header.jpgs. That is what I meant.

Re: More Randomer

2008-06-02 09:35 • by Biggle (unregistered)
198324 in reply to 198287
Eric:
13 is not random enough. Only 13! Should have done 26 then it would be even more random... oh hold on...


To make it truly randomerer, he should use a random number of random numbers.

Re: More Randomer

2008-06-02 09:42 • by Anon. (unregistered)
198327 in reply to 198312
jtl:
See, my randomizer goes to 11.

int randomizer () {
return 11;
}

Re: More Randomer

2008-06-02 09:43 • by ChZEROHag
198329 in reply to 198324
Biggle:
Eric:
13 is not random enough. Only 13! Should have done 26 then it would be even more random... oh hold on...


To make it truly randomerer, he should use a random number of random numbers.


But what should the upper and lower bound be with which to choose the random number to choose a random number of random numbers of?

Re: More Randomer

2008-06-02 09:43 • by CP (unregistered)
Of course, if he was smart, he would've generated this code...

Kinda makes one wonder, what could such a generator have looked like...

Re: More Randomer

2008-06-02 09:48 • by yetihehe (unregistered)
Seriously, everybody knows that bigger sample equals better results in statistics.~

Re: More Randomer

2008-06-02 09:48 • by th30519
198332 in reply to 198327
Anon.:
jtl:
See, my randomizer goes to 11.

int randomizer () {
return 11;
}


int random() {
return MAX + 1;
}

</spontaneous combustion>

Re: More Randomer

2008-06-02 10:35 • by snoofle
198344 in reply to 198329
ChZEROHag:
Biggle:
Eric:
13 is not random enough. Only 13! Should have done 26 then it would be even more random... oh hold on...


To make it truly randomerer, he should use a random number of random numbers.


But what should the upper and lower bound be with which to choose the random number to choose a random number of random numbers of?
[42,42]

Re: More Randomer

2008-06-02 10:45 • by DL (unregistered)
He asks for a random number from 1 through 13 but then the conditionals test 0 through 12. It only works for 13 because of the unconstrained final 'else' which doesn't even conceptually fit since he was clearly trying to itemize each possible result.

Dorkus.

Re: More Randomer

2008-06-02 10:47 • by CrazyFrog (unregistered)
I've always loved Troll-like programming!

Re: More Randomer

2008-06-02 10:59 • by Cowbert (unregistered)
How do we know this wasn't originally built with 13 different images that were supposed to be rotating, but gradually got replaced down to 2? (Think banner ad rotation).

Captcha: ingenium (is this the new version of 'brillant'?)

Re: More Randomer

2008-06-02 11:20 • by randoom (unregistered)
This sure makes it easy to guess the output of the "random" generator
1. DateTime.Now.Millisecond has a value of 0 to 999 because it only returns the millesecond portion of the time. If you make a request at 12:00:00.000 and 12:00:01.000 you will have the same "random" number.
2. If two requests are made within one millisecond, they will have the same random number.

CAPTCHA: Vulputate

Re: More Randomer

2008-06-02 11:27 • by More (unregistered)
Hmmm. I have a theory. The programmer originally had:

Dim rNumber As Integer = 0
rNumber = RandomNumber(1, 2)
Select Case rNumber
Case 0
Me.hl_logo.ImageUrl = "~/sysimages/header.jpg"
Case 1
Me.hl_logo.ImageUrl = "~/sysimages/header2.jpg"
Case Else
Me.hl_logo.ImageUrl = "~/sysimages/header2.jpg"
End Select


And for “some reason” it always returned header2.jpg. He then noticed that by “adding” “more randomness” to the select statement he got header.jpg more

Re: More Randomer

2008-06-02 11:31 • by Andreas (unregistered)
Harking back to the time when I cared about such things: the least significant bits of a certain class of pseudo random generators are less random that the full stream (If you really want to know, read Knuth's "The Art of Computer Programming", Vol 2)

Re: More Randomer

2008-06-02 11:42 • by real_aardvark
198360 in reply to 198359
Andreas:
Harking back to the time when I cared about such things: the least significant bits of a certain class of pseudo random generators are less random that the full stream (If you really want to know, read Knuth's "The Art of Computer Programming", Vol 2)
What, the whole volume?

I may have to take an extra-long coffee-break for this one.

Re: More Randomer

2008-06-02 11:55 • by JustChris (unregistered)
"This MD5 hash isn't secure enough. Do a recursive loop to MD5 it 5 times over."

Re: More Randomer

2008-06-02 12:35 • by kluizenaar (unregistered)
Well, in fact it is true that the lower bits of most random functions contain less entropy than the higher bits.


Captcha: gravis (ftw!)

Re: More Randomer

2008-06-02 12:49 • by Beo (unregistered)
198382 in reply to 198351
I'm not sure what's more depressing -- that this was my natural assumption (from years working with clients who were always right) or that someone else beat me to it...

Re: More Randomer

2008-06-02 12:56 • by alegr
198386 in reply to 198364
JustChris:
"This MD5 hash isn't secure enough. Do a recursive loop to MD5 it 5 times over."


Ever heard of triple DES?

Re: More Randomer

2008-06-02 13:06 • by Ken B (unregistered)
198387 in reply to 198344
snoofle:
ChZEROHag:
Biggle:
Eric:
13 is not random enough. Only 13! Should have done 26 then it would be even more random... oh hold on...
To make it truly randomerer, he should use a random number of random numbers.
But what should the upper and lower bound be with which to choose the random number to choose a random number of random numbers of?
[42,42]
I think you'll find much better results with
(41,43)

Re: More Randomer

2008-06-02 13:12 • by Monkey Brains (unregistered)
It's awesome that the developer's goal was to be more random rather than to show each header half the time. Too bad he failed at both.

Re: More Randomer

2008-06-02 13:12 • by kris (unregistered)
198391 in reply to 198329
ChZEROHag:
Biggle:
Eric:
13 is not random enough. Only 13! Should have done 26 then it would be even more random... oh hold on...


To make it truly randomerer, he should use a random number of random numbers.


But what should the upper and lower bound be with which to choose the random number to choose a random number of random numbers of?


Obviously some random numbers.

Re: More Randomer

2008-06-02 13:57 • by Manic Mailman (unregistered)
As a couple people already mentioned a secondary WTF is that the random number generator gets reseeded with each call to RandomNumber(). Reseeding RNGs unnecessarily is a WTF that I see all the time - please people, stop doing that!

Seed your RNG once and only once. In .NET this means having only a single instance of the Random class - make it a static if you can't figure out a better way to do it. If you think you need to reseed your RNG very time you want a random number, you're wrong. If you think you *ever* need to reseed your RNG more than once, explain why (in comments if not to an actual other human being). If you can't coherently explain why, even to yourself, then you don't need to do it.

Re: More Randomer

2008-06-02 14:15 • by ounos
198408 in reply to 198295
alex:
And he reinitializes the random number generator every time !

It's way more random that way.

Re: More Randomer

2008-06-02 14:37 • by Wonko the sane (unregistered)
198410 in reply to 198301
Proko:
Well, I know a guy who tried to explain that using:
(random(max_number)*random(max_number)) mod max_number
is much more random usage then simply random(max_number). He didn't even care when one other guy proved mathematicly, that he was totally wrong.


I've just tested this with dice...

OK used a table of 1-6, and 1-6 all combinations...

and the results were
1's - 2
2's - 6
3's - 5
4's - 6
5's - 2
0's - 15

Not an even mix is it...

Re: More Randomer

2008-06-02 16:11 • by DWalker59
Wonko the sane: Strange dice have you, with no spots on one side... but why would the side with no spots (which is obviously heavier) come up on top so much of the time?

Ponder this, we must.

Re: More Randomer

2008-06-02 16:21 • by Si. (unregistered)
Loads of WTFs.
- Random method.
- Not using constants.
- 13 case switches for only 2 outcomes.
- "can also throw exception or return 0" ????

Someone needs to teach this kid the law of large numbers.
« PrevPage 1 | Page 2Next »

Add Comment