Question Random number in range is outside range

rosemary

New member
Joined
Dec 14, 2009
Messages
3
Programming Experience
10+
I am using the following code in the Load method of a VB form to generate random numbers in the range 1 to 8. Without fail after enough loops the range is exceeded and 9 is the random number returned every time. The line using the Rnd function is from:

Rnd Function (Visual Basic)

Randomize()
Dim LoopCount As Integer = 0
Do
GoldButtonNumber = CInt(Int((8 * Rnd()) + 1))
LoopCount += 1
Loop Until GoldButtonNumber > 8


From the documentation it makes sense to me as the Rnd number generated could be less than, but very close to 1, which when multiplied by 8 will be very close to 8. It will be rounded up to 8 when converted to an integer and when 1 is added will be 9. What am I missing?

Thanks,
Rosemary
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,393
Location
Sydney, Australia
Programming Experience
10+
Don't use Rnd(). For random numbers use the Random class. If you want a random number in the range 1 to 8 then you just do this:
VB.NET:
myInteger = myRandom.Next(1, 9)
 

send2te

Well-known member
Joined
Dec 13, 2009
Messages
46
Programming Experience
10+
GoldButtonNumber = CInt(Int((8 * Rnd()) + 1))
Here GoldButtonNumber can take a maximum value of 8 and this is an infinite loop.

RND() can equal to zero but always less than 1.

8 * Rnd() can equal to zero but always less than 8.

Int(8 * Rnd()) can equal to zero but alway less than or equal to 7.

Int((8 * Rnd()) + 1 can equal to zero but alway less than or equal to 8.

CInt(Int((8 * Rnd()) + 1)) will produce always the same result - This is just an extra CInt.


So Loop is Infinite....

Use 9 at the place of 8.

:) but now you final result will always a constant value ie. 9. after going out from the loop.

What exactly you want - Please let me know.
 

rosemary

New member
Joined
Dec 14, 2009
Messages
3
Programming Experience
10+
Don't use Rnd(). For random numbers use the Random class. If you want a random number in the range 1 to 8 then you just do this:
VB.NET:
myInteger = myRandom.Next(1, 9)

Thanks - however if you read the Random class to be safe one is recommended to override at least 2 methods which seems like overkill for a simple program such as I'm writing. Any idea why using the Rnd() function doesn't work - it is quoted in the MSDN and all over the web. Is there something I am missing?
 

rosemary

New member
Joined
Dec 14, 2009
Messages
3
Programming Experience
10+
GoldButtonNumber = CInt(Int((8 * Rnd()) + 1))
Here GoldButtonNumber can take a maximum value of 8 and this is an infinite loop.

RND() can equal to zero but always less than 1.

8 * Rnd() can equal to zero but always less than 8.

Int(8 * Rnd()) can equal to zero but alway less than or equal to 7.

Int((8 * Rnd()) + 1 can equal to zero but alway less than or equal to 8.

CInt(Int((8 * Rnd()) + 1)) will produce always the same result - This is just an extra CInt.


So Loop is Infinite....

Use 9 at the place of 8.

:) but now you final result will always a constant value ie. 9. after going out from the loop.

What exactly you want - Please let me know.

I think you have missed the point - there is no infinite loop just a problem with the random number returned sometimes being outside the specified boundary. Thanks anyway.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,393
Location
Sydney, Australia
Programming Experience
10+
Thanks - however if you read the Random class to be safe one is recommended to override at least 2 methods which seems like overkill for a simple program such as I'm writing. Any idea why using the Rnd() function doesn't work - it is quoted in the MSDN and all over the web. Is there something I am missing?
I have no idea what you mean. There's no overriding anything. You sinmply create a Random object and call Next. That's it. Why would you call Rnd and then have to shift and scale the result when Random.Next is simpler?
 

send2te

Well-known member
Joined
Dec 13, 2009
Messages
46
Programming Experience
10+
I think you have missed the point - there is no infinite loop just a problem with the random number returned sometimes being outside the specified boundary. Thanks anyway.

{Be hAppy}

This is infinite loop as per mathematics because RND() value can not be equal to one so the written can not bring 9 mathematically. Actually it should be infinite loop theoratically But practically its not because of computational limitation...

Let me explain

In following code you will never receive the message "Not Infinite Loop"
===========
Randomize()
Dim LoopCount As Integer = 0
Dim GoldButtonNumber As Integer = 0
Dim h As Double
Do
h = Rnd()
GoldButtonNumber = CInt(Int((8 * h) + 1))
LoopCount += 1
Loop Until GoldButtonNumber > 8
MsgBox("Not Infinite Loop")
===========
Just change the line

Dim h as Double
as
Dim h as Single

and now you will receive the message.


If you say

If 0.1 + 0.1 = 0.2 then
msgbox "Yes"
end if

you will receive the message yes

but if you say

If 0.1 + 0.1 + 0.1 = 0.3 then
msgbox "Yes"
end if

then you will receive no message ...
 

send2te

Well-known member
Joined
Dec 13, 2009
Messages
46
Programming Experience
10+
{Be hAppy}

say

min= 1000
max = 9999
if you want to pick any value between this range then use:

x=min+int(rnd()*(max-min+1))

Then x will always be a number between 1000 and 9999
 

wowjen

Member
Joined
Dec 27, 2009
Messages
13
Programming Experience
Beginner
Randomize()
txt1.Text = Int(Rnd() * 8)


will this be a help?
 
Top Bottom