
kinda dumb question
ok generating random numbers. should the lowerbound be 1 lower than the actual lower bound or am i wrong here? i notice that when i make my bounds the max and min values the lower bound never appears, but as soon as i set the lowerbound to 1 less than the min, within a few tries i see the lowerbound. here's what i'm using:<BR><BR>iNum = int(((cUBound  cLBound + 1) * rnd + cLBound))

Do the math...
Consider: The RND() function returns a number that is greater that *OR* equal to zero but is less than one.<BR><BR>That is, 0.0 <= RND() < 1.0<BR><BR>Now, you multiply RND() by some integer. Say N. So what is the result? <BR> 0.0 <= ( RND() * N ) < N.0<BR>Right?<BR><BR>Because N times zero will still be zero, so you *can* get a zero result. But N times a number just a tiny bit less than one will be a result just a tiny bit less than N. In other words, you'll never reach N.<BR><BR>So let's say N is 5. Then look at this table of possible values of 5 * RND() given the value of RND() shown. To save time, I'm also going to show the result when we take the INT() of the multiplied result:<BR><BR>RND()  ( 5 * RND() )  INT( 5 * RND() )<BR>0.001  0.005  0<BR>0.010  0.050  0<BR>0.100  0.500  0<BR>0.150  0.750  0<BR>0.200  1.000  1<BR>0.201  1.005  1<BR>0.300  1.500  1<BR>0.400  2.000  2<BR>0.600  3.000  3<BR>0.800  4.000  4<BR>0.900  4.500  4<BR>0.999  4.995  4<BR><BR>I hope you can see that, by starting with 0 <= RND() < 1, we end up with a random integer in the range from zero to one less than the number we multiplied by.<BR><BR>Now look back at the formula:<BR><BR>NumFromLtoH = L + INT( RND() * ( H  L + 1 ) )<BR><BR>Do you now see why it works???<BR><BR>Say you want to throw a die...get a result from 1 to 6. So L=1 and H=6. SO:<BR><BR>NumFromLtoH = L + INT( RND() * ( H  L + 1 ) )<BR>>><BR>NumFromLtoH = 1 + INT( RND() * ( 6  1 + 1 ) )<BR>>><BR>NumFromLtoH = 1 + INT( RND() * 6 )<BR><BR>Remember, INT( RND() * 6 ) will give a result from 0 to 5. [6 possible results, but *always* starting at zero!] Then you add on the 1, and you have a result from 1 to 5.<BR><BR>Does that help at all???<BR><BR>[You will note that it doesn't matter whether you add on the lower bound before or after you do the INT. After all, the lower bound is already an integer, so INT doesn't affect its value. I like putting it outside the INT to emphasize that it isn't really a part of the random number calculations but is just used to bring the result up to the starting number wanted. But your mileage may vary.]<BR><BR>

Dork...
> Then you add on the 1, and you have a result from 1 to 5.<BR><BR>Learn to do arithmetic, Wilkinson!<BR><BR>It should have read:<BR><BR>Then you add on the 1, and you have a result from 1 to 6.<BR><BR>****************<BR><BR>And I didn't explain why we add 1 to the difference of H and L. I presume you understood that point? 1 to 6 is a range of 6 numbers, but 61 gives only 5: the difference. The range is always one more than the difference.<BR><BR><BR><BR>

thanks bill
thanks for the exlanation. im feeling a bit embarassed, but glad to know the answer now =^]<BR>justin
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

