Qbasicnews.com
July 20, 2019, 11:25:12 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]
  Print  
Author Topic: random numbers  (Read 2213 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


WWW
« 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


WWW
« 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]
  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!