Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Random #'s in VB

This topic is 5465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need a little help generating random integer values in VB6. I unserstand Randomize and Rnd. Here is my problem: If i''m trying to randomize an integer between say 0 and 5, I''ve been to told to use: variable = Round(Rand*5). However, wouldn''t the numbers 0 and 5 be half as likely to be chosen as all the others, since they are missing half the "range" that the others have? 0 dones''t have the discrepence of 0.5 below it and above for 5. I''m thinking if I want to randomize numbers between 0 and 5, I would use Round(Rand*6 - 1), to get something between -1 and 5, so I can use the half range for -1 and the half range for 5 to make the probability of 5 equal to the rest of the numbers, while giving 0 a full range. Is this a good method, and was I correct in my "discovery"?

Share this post

Link to post
Share on other sites
I''ve been using Visual Basic since it''s first implementation and I''m sorry to say I''m not 100% what your asking. I understand that your trying to get a random number... so here''s the basic primer I give my friends when they ask how.

First off, you''ll want to seed the random number generator because if you don''t you''ll get the same numbers every time the program runs...

Randomize Timer

and that''s through with.
Rnd returns a number between 0 and 1, so you merely multiply
it by the top most desired result to get a number between 0 and

A random number between 0 and 5...

MyNumber = rnd*5

Now, there''s a lot of problems with that. For example, rnd never actualy reaches 1 and because of that you''ll never get 5.
You''ll also be getting decimals at the end (unless of course MyNumber is an integer)

This is how I do it myself in my own games.

MyNumber = int(Rnd * HighestDesiredNumber) + 1

With that, you never get a 0, but rather a number that is 1,2,3,4, or 5 (assuming you put 5 in place of HighestDesiredNumber).

I hope that helps, it''s a lot easier to explain in person.

Share this post

Link to post
Share on other sites

Public Function RandomNumber(ByVal Lowest As Long, ByVal Highest As Long) As long
RandomNumber = (Highest - Lowest + 1) * Rnd + Lowest
End Function

x = RandomNumber(5,29) ' Gets a random number from 5 to 29.   

Rob Loach
Website: Over-Development
Current Project: Pong, The First

"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed. "
- The Boondock Saints

[edited by - Rob Loach on November 6, 2003 3:39:54 AM]

Share this post

Link to post
Share on other sites
Hi gamechampionx

You are correct that rounding a number "to the nearest" anything will limit the spread of your extremities by a half step.

However, the sensible way around this is to make each value have an equal likelihood of selection. In your example, each integer value should have a range of 1. Using "Round", the range for integer value 3, would be >=2.5 to <3.5 - and it is precisely this which causes your problem around 0.

But why does it matter to you which part of a fraction ends up allocated to which integer?

Instead of rounding to the nearest, how about always rounding down? That way, given a range of 0 to <6, the number 0, 1, 2, 3, 4 and 5 all have an exact range of 1.

That is why you should use = int(rnd()*6)

Hope that clarifies

Share this post

Link to post
Share on other sites
Oh... lol that''s what the question was... No, TMHill is right. int(rnd*5)+1 works perfectly well since you always round down. *feels stupid* I forgot there even was a Round() function (assuming there is)... I''ve never used it before as I''ve always found it better to just Int() a value.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!