Qbasicnews.com
July 07, 2020, 02:39:34 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: Need help creating very simple program  (Read 2813 times)
PrimeX
New Member

Posts: 1


« on: October 23, 2004, 01:23:15 AM »

I just want to create a program that allows me to generate a random number between 1 and 10. Can anyone help? All help is appreciated.
Logged
Sterling Christensen
Na_th_an
*****
Posts: 1328


« Reply #1 on: October 23, 2004, 03:19:15 AM »

Check out the RND function.
Logged
computerkid14
Forum Regular
**
Posts: 143



« Reply #2 on: October 23, 2004, 10:07:35 AM »

you will need the CINT command.
there are tons a tutorials out there that explain how to, www.qbasic.com has one, if you need help understanding how it works, let us know.
(i actually know about this one!!!! wohoooo) :lol:
Logged
TheBigBasicQ
*/-\*
*****
Posts: 4550



WWW
« Reply #3 on: October 23, 2004, 12:30:43 PM »

example:

D1 = INT(RND * 6) + 1

the above statement will generate a random no. between 1 and 6 =P.
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #4 on: October 23, 2004, 12:44:32 PM »

[syntax="QBasic"]Number = INT(RND * (highbound - lowbound + 1)) + lowbound[/syntax]
I thought that was the formula for random generation of a number between two integer arbitrary values highbound and lowbound Wink
Logged
moochthemonkey
I hold this place together
*****
Posts: 801



« Reply #5 on: October 23, 2004, 03:39:56 PM »

actually, to just make it simple heres the answer

Code:

r = INT(RND * 10) + 1
PRINT r


but you will notice that each time you restart the run the program you will come up with the same number each time! To get different numbers you will need to reseed the generator with the RANDOMIZE command. So you're complete program will look like this:

Code:

RANDOMIZE TIMER
r = INT(RND * 10) + 1
PRINT r
Logged

quote="na_th_an"]
Greenday, Spice Girls... Can you tell the difference?
[/quote]
Moneo
Na_th_an
*****
Posts: 1971


« Reply #6 on: October 23, 2004, 11:31:45 PM »

Neo had the right formula.
Here's a function showing how to do this precisely.
Code:
 DECLARE FUNCTION RandInt% (lower, Upper)
' =============================== RandInt% ===================================
'   Returns a random integer greater than or equal to the Lower parameter
'   and less than or equal to the Upper parameter.
' ============================================================================
'
FUNCTION RandInt% (lower, Upper) STATIC
   RandInt% = INT(RND * (Upper - lower + 1)) + lower
END FUNCTION

It turns out that the code for random numbers from 1 to 10 looks the same as what Offensive posted. But if you wanted numbers from 2 to ten, the modifying of Offensive's code would not be obvious unless you had Neo's formula or my function.

YOU OTHER GUYS: If you're not really going to help, don't just give partial hints. The suggestion to use the CINT was no help at all. Also, referring to tutorials is the same as RTFMing.
*****
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #7 on: October 24, 2004, 07:24:56 AM »

Note that all about Random Numbers will be explained in QB Now! Part II (which will be online in Pete's QB Express from December 15th). The chapter which includes random numbers is 6 A4 pages is size.

Moneo:
Thanks Wink
Logged
TheBigBasicQ
*/-\*
*****
Posts: 4550



WWW
« Reply #8 on: October 25, 2004, 09:32:31 AM »

what is in there?? Tutorial on finding the pattern of random numbers? =P
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #9 on: October 25, 2004, 10:39:34 AM »

No, it just explains how to use RND, RANDOMIZE and everything else that has to do with random numbers Wink
Logged
HQSneaker
Wandering Guru
***
Posts: 394



WWW
« Reply #10 on: October 25, 2004, 10:45:16 AM »

Huge Cheesy
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #11 on: October 25, 2004, 10:46:26 AM »

6 A4 pages is indeed pretty large for one topic Wink
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!