Qbasicnews.com
Fiddling around with random #s and odds. - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Fiddling around with random #s and odds. (/thread-9853.html)

Pages: 1 2 3


Fiddling around with random #s and odds. - Zack - 01-08-2007

I was fiddling around with chances, RND, and loops, and I noticed an abnormality...I think. Run this code:
Code:
dim as integer n
randomize timer
do until n=1234567
    n=int(rnd*2000000)+1
loop
So the program should terminate until the random number 1234567 is generated in the loop. The chances of that happening on one iteration are 2 million to 1 against (because I'm generating a number between 1 and 2,000,000). Now, with a modern computer, millions of iterations should run within a few seconds. After 2,000,000 iterations, it is probable that 1234567 should have come up, and the loop should have ended. But when I run the program it never seems to happen, even after several minutes (after which possibly billions of iterations have been executed, and therefore 1234567 almost certainly should have been generated). Why is this?
Either a) I'm missing a very obvious progamming error, b) INT(RND*2000000)+1 is weighting certain numbers as more probable, c) I've made some sort of math error, which I doubt because the odds are fairly simple, or d) my computer is really slow at executing the loop.
Which is it?


Fiddling around with random #s and odds. - Moneo - 01-08-2007

You DIM'ed "n" as an integer.

A positive integer only goes up to 32,767, assuming FB integers are 16 bits like in QB.

Try the DIM as long.

Regards..... Moneo


Fiddling around with random #s and odds. - stylin - 01-08-2007

Try reading this: http://www.freebasic.net/wiki/wikka.php?wakka=CatPgStdDataTypes


Fiddling around with random #s and odds. - Anonymous - 01-08-2007

Executes and quits within 1 second here.

LONG and INTEGER are synonyms in FB, which is a 32-bit compiler Wink


Fiddling around with random #s and odds. - Zack - 01-08-2007

Quote:You DIM'ed "n" as an integer.

A positive integer only goes up to 32,767, assuming FB integers are 16 bits like in QB.

Try the DIM as long.

Regards..... Moneo
INTEGERs are 32 bits in FB, as the others said.
@cha0s: so what's going on? My computer won't generate 1234567, but yours will? Big Grin


Fiddling around with random #s and odds. - Dio - 01-09-2007

i ran the following code on my computer and unless i made a mistake (unfathomable, i know :roll: ) it won't generate 1234567 for me either.

Code:
dim as long n
randomize timer
s = timer
do until n=1234567
    n=int(rnd*2000000)+1
loop
? done
? timer - s

EDIT: strange, i changed the 1234567 to 123456 and then 2000000 to 200000 and it worked like a charm. :???:


Fiddling around with random #s and odds. - Skyler - 01-09-2007

And this is useful how?


Fiddling around with random #s and odds. - Moneo - 01-09-2007

Zack, I compiled and ran the following using QuickBasic 4.5,
and it found 1234567 in about 3 seconds.

Regards..... Moneo

Code:
dim n as long
dim upper as long
upper=2000000

randomize timer

do until n=1234567
    n=int(rnd*upper)+1
loop
print n
system



Fiddling around with random #s and odds. - Zack - 01-09-2007

Quote:i ran the following code on my computer and unless i made a mistake (unfathomable, i know :roll: ) it won't generate 1234567 for me either.

Code:
dim as long n
randomize timer
s = timer
do until n=1234567
    n=int(rnd*2000000)+1
loop
? done
? timer - s

EDIT: strange, i changed the 1234567 to 123456 and then 2000000 to 200000 and it worked like a charm. :???:
That should make it quicker, because the chances of getting the number are now 10x better. Still should work the other way, though.
Moneo: So it really is just my computer. Strange.


Fiddling around with random #s and odds. - Skyler - 01-09-2007

That's what happens when you run Windows...
Confusedigh: