subroutine for adding random generated numbers

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Sgledhill5
Newbie
Posts: 2
Joined: Mon Feb 25, 2008 6:06 pm

subroutine for adding random generated numbers

Post by Sgledhill5 » Mon Feb 25, 2008 6:15 pm

Are there any Qbasic Programmers who can provide me with a subroutine for adding random generated numbers, thanks

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: subroutine for adding random generated numbers

Post by moneo » Mon Feb 25, 2008 8:22 pm

Sgledhill5 wrote:Are there any Qbasic Programmers who can provide me with a subroutine for adding random generated numbers, thanks
DECLARE FUNCTION RandInt% (Lower, Upper)
RANDOMIZE TIMER 'Put this at the top of your program to seed the RND function.


' =============================== 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


NOTES AND QUESTIONS:
1) The above function will work easier than a subroutine.

2) The way the function is set up, the maximum random number is 32767, that is, an integer.

3) You will need to set up the Lower and the Upper parameters.

4) How do you want to handle duplicate random numbers, process them or eliminate them?

5) What do you mean by ADDING random generated numbers? Adding them together, adding them to an array?

Regards..... Moneo
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.

User avatar
burger2227
Veteran
Posts: 2378
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Try calling that!

Post by burger2227 » Tue Feb 26, 2008 1:03 am

To create a Random number, you just use RND. But you have to set the number limits first.

RND returns a number from 0 to .9999999 by itself. That is not a great range of numbers.

So now you have to set a maximum number by multiplying the RND by the maximum number needed. Most times you need an Integer answer so you get the result using the INT() function. There is a problem there, because value = INT(RND * 65) will never give you 65.

Adding 1 to the equation creates numbers from 1 to 65.

To get values from 0 to 65 you need to multiply by 66 using INT. The value cannot be the maximum when INT is involved because it rounds down. You could use CINT or CLNG to round to the closest number.

To create really random numbers, use RANDOMIZE TIMER before using RND. Otherwise, QB will always send the same sequence of numbers everytime the program is run. The TIMER seeds the RND sequence to the actual second of the day. That is 86400 different seeds!

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Post Reply