10-10-2005, 01:38 PM
Hi, ok i know this may sound pretty stupid but i'm a total n00b to VB and am doing a course in it but right now i am only wondering one thing.
Does any one know how to get a random value between a set of 2 numbers?
I'm trying to write a game program which will do the dice rolls for a dungeons and dragons game.
10-10-2005, 02:57 PM
dim rand as new random
'say its a nice simple 6 sided die and not a weird D&D one
'this produces ints between 1 and 6
dim throw as integer = rand.next(1,7) ' add one to the top value.
10-10-2005, 08:28 PM
i find it much easier to do it this way
Dim i as integer = rnd()*5+1
rnd() will generate a random number between 0 and 1, and then u just muliply that number by 5, which will give u a number between 0 and 5, so then u just add one. Just make another var for the second dice, so in thoery u could replace i with dice1, and then do another for dice2, however u want. I find this much easier than the way most ppl do it
10-10-2005, 11:39 PM
You have more control over the *randomness* with Type Random though, than simply rnd(). With jo0ls' method you can set the seed of the random number (you can use the date, or Environment.TickCount), which results in a more *random* number.
As you may or may not know, there is no such thing as a true random number from a digital computer. You always have to base the number on something. This something is called the 'seed'. The larger your seed (eg, 16it, 32bit....2024bit I have seen at www.paradisepoker.com), the closer to *random* your random number will be.
Notice with System.Type Random you will always get the same order of numbers with jo0ls' method, unless you change the seed. This is more correct than rnd(), since you have control over the seed.
10-11-2005, 06:48 AM
Note: The Rnd() function is a legacy function... you should be using the .NET version, which is the Random class.
However, just like in VB6, the .NET option lets you seed it or it will automatically seed... but that is when you instantiate it.
So, just like Randomize should only be called once in an application, you should only create one instance of a Random class in your application.
Dim Ri As Random 'declaration
Ri = New Random 'possibly in Sub New, Sub Main, or Form_Load.
And, of course
X = Ri.Next(A, B) 'random number < B but >= A.
to include B in the range.
X = Ri.Next(A, B+1)
to exclude A from the range.
X = Ri.Next(A+1, B)