Qbasicnews.com
April 12, 2021, 07:40:29 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: Fiddling around with random #s and odds.  (Read 15173 times)
Zack
*/-\*
*****
Posts: 3974



WWW
« on: January 07, 2007, 08:59:37 PM »

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?
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #1 on: January 08, 2007, 02:03:48 AM »

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
Logged
stylin
Ancient QBer
****
Posts: 445


« Reply #2 on: January 08, 2007, 10:23:58 AM »

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

stylin:
Anonymous
Guest
« Reply #3 on: January 08, 2007, 10:30:07 AM »

Executes and quits within 1 second here.

LONG and INTEGER are synonyms in FB, which is a 32-bit compiler Wink
Logged
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #4 on: January 08, 2007, 12:46:06 PM »

Quote from: "Moneo"
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? Cheesy
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Dio
I hold this place together
*****
Posts: 874



« Reply #5 on: January 08, 2007, 06:15:16 PM »

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.  :Huh:
Logged

quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Skyler
Ancient Guru
****
Posts: 564



« Reply #6 on: January 08, 2007, 06:45:42 PM »

And this is useful how?
Logged

In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Moneo
Na_th_an
*****
Posts: 1971


« Reply #7 on: January 08, 2007, 09:07:42 PM »

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
Logged
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #8 on: January 09, 2007, 12:12:39 PM »

Quote from: "Dio"
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.  :Huh:

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.
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Skyler
Ancient Guru
****
Posts: 564



« Reply #9 on: January 09, 2007, 12:47:07 PM »

That's what happens when you run Windows...
:sigh:
Logged

In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Dio
I hold this place together
*****
Posts: 874



« Reply #10 on: January 09, 2007, 05:50:55 PM »

wait a minute, why doesn’t this work?

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


also,

i ran the following code in qbasic.

Code:
RANDOMIZE TIMER
s = TIMER
celing& = 2000000
DO: LOOP UNTIL INT(RND * celing&) + 1 = 1234567
PRINT done
PRINT TIMER - s: SLEEP



it successfully found 1234567 in:

first run completed in:

3.5 seconds

second:

2) 1/5 of a second

henceforth:

3) .75 of a second

4) 3.8 seconds

----

regardless of the amount of time it took, it still worked.
Logged

quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #11 on: January 09, 2007, 06:34:07 PM »

So:
-it works in QB
-it doesn't work in FB, at least on Windows.
So it's an FB problem...perhaps FB's random function is weighting certain ranges. Can somebody with a Linux build of FB try it?
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #12 on: January 09, 2007, 09:51:34 PM »

"Freebasic is like QB, except it doesn't work" LOL.

*****
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #13 on: January 10, 2007, 11:40:20 AM »

The only number between 1234500 and 1234600 I found using your programs and FB is 1234559.

I think FB's RND function or internal calculations is lacking precision.
Logged
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #14 on: January 10, 2007, 12:04:12 PM »

Thanks Neo (how've you been, by the way? Aren't you taking aerospace studies or something?).
I guess somebody should tell the FB people about this.
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!