|
-
Nov 25th, 1999, 05:13 PM
#4
Member
I've worked all this out before but unfortunately my VB PC has completely died so I can't get to any of the code Think this is right...
Consider this function to generate a random number between known values:
Public Function BoundedRND(ByVal iMin as Integer, ByVal iMax as Integer) as Integer
BoundedRND = Int((iMax - iMin + 1) * Rnd + iMin)
End Function
This gets a random number in a range (iMax - iMin + 1), in your example 0 to 4, then adds the lower number (in your example 1) to put the returned range within the desired range.
In the situation you're talking about, you are after an integer between 1 and 5: applying in BoundedRND gives:
BoundedRND = Int((5 - 1 + 1) * Rnd + 1)
= Int(5 * Rnd + 1)
= 1..5
So - the "+ 1" is just putting the random number so far generated within the bounds 1..n.
Make sense?
AndyC
London
------------------
* * * * * * * * * * * * * * * * * * * * * *
* *
* AndyC *
* London *
* email: [email protected] *
* *
* * * * * * * * * * * * * * * * * * * * * *
[This message has been edited by Andy Collyer (edited 11-26-1999).]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|