Qbasicnews.com
August 13, 2020, 09:34:17 PM
 Pages: [1]
 Author Topic: random numbers  (Read 2475 times)
MN
New Member

Posts: 1

 « on: October 20, 2007, 01:28:54 PM »

hope someone could help
looking to generate random numbers from 0-9
my problem is they come up the same and i could never get the 0 to show up
 Logged
Mac
Senior Member

Posts: 243

 « Reply #1 on: October 20, 2007, 04:19:22 PM »

looking to generate random numbers from 0-9

randomize timer
for i=1 to 50: print int(rnd*10);: next i: print

This will generate 50 random numbers and the series will be different the next time you run the program because of the randomize timer statement at the beginning of the program.

But note also that by the nature of random numbers, I could theoretically generate all 50 and not get a 0 or a 7 or any other given digit. It is a bit like flipping a coin: you could get 50 heads.

If what you want is all 10 digits, 0-9, in random order with each digit appearing exactly once, ask again. That is a different technique.

Mac
 Logged
Mac
Senior Member

Posts: 243

 « Reply #2 on: October 20, 2007, 04:24:40 PM »

'Namely
DIM d(9) AS INTEGER: FOR i = 1 TO 9: d(i) = i: NEXT i
RANDOMIZE TIMER
FOR shuffle = 1 TO 5' Just shuffling 5 times. Could be any number."
FOR i = 0 TO 9
r = INT(RND * 10)' A random number 0 to 9
SWAP d(i), d(r)
NEXT i
NEXT shuffle
FOR i = 0 TO 9: PRINT d(i); : NEXT i: PRINT

 Logged
 Pages: [1]