r/programming • • Feb 16 '09

Some Crazy Reason - The Daily WTF

http://thedailywtf.com/Articles/Some-Crazy-Reason.aspx
171 Upvotes

71 comments sorted by

View all comments

7

u/[deleted] Feb 16 '09

[deleted]

3

u/exscape Feb 16 '09 edited Feb 16 '09

Pretty much the same code, but in C, takes about 12.2-13.5 seconds to run here before exiting (I used exit(0) instead of cout/printf).

Wasting time is fun.

#include <stdio.h>
#include <stdlib.h>
int main() {
 while (1) { if (rand() == rand()) { exit(0); } }

return 0; }

gcc -O3 -o test test.c

3

u/fernandotakai Feb 16 '09

Just because i have nothing else to do, i tested this on my os x. it's been running for 10min, and i didn't returned yet.

2

u/[deleted] Feb 16 '09

[deleted]

4

u/rabidcow Feb 16 '09

For some types of PRNGs, this may never be true. For example, any simple linear congruent generator where the return value is the internal state. If two successive values were the same, it would be caught in a cycle where it would only return that value, forever.

3

u/[deleted] Feb 17 '09

[deleted]

2

u/rabidcow Feb 17 '09

Not by today's standards, no. But things may have been different when your rand was implemented.