The (pseudo) random numbers created by this are always the same. Can I change that?
Code:Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd) + Low
End Function
Printable View
The (pseudo) random numbers created by this are always the same. Can I change that?
Code:Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd) + Low
End Function
Would this suffice?
Edit - Also, try including the last low in the CInt conversion and see if that works for you:Code:Public Function randomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
Dim r As New Random
randomNumber = r.Next(low, high)
Return randomNumber
End Function
I noticed it looked almost like the example given in the MSDN's library on the Rnd() function, but not quite.Code:Rand = CInt((High - Low + 1) * Rnd() + Low)
Have you executed Randomize once and once only? What Low and High values are you using? Does it work for Integers instead of Longs? Do you definitely need to use Longs?
Don't do this. This is wrong. Do not construct the Random instance in the function. Your application should only ever create one Random instance and all places that need to use it should be passed a reference. With the above function, if it is called twice in succession it will return the same number, as both calls will seed the Random with the same value.
I also tried this and they were always the same either. This is actualy randomizing a number for 3 diferent variables and you can keep clicking a button which would re randomize there number.
Code:randomnumber = (Str(Int(Rnd() * 11 + 1)))
How come you can post back but you can't answer my questions? I'd kinda like to help but can't without information.
Well I dont know if I executed it once and only once.(Im not that good with vb) My low is 1 my high is 12. I have not tried with longs. But explain why if I did why would that work better then an integer?
Surely you know whether you executed it at all. If you haven't then obviously you haven't executed it once. If you have executed it then could you not simply put a MessageBox.Show right after it and if you only see that once then you know that you've only executed it once? Failing that could you not show us where it is and let us try to work it out? Don't make us drag every little bit of information out of you.Um, you HAVE tried it with Longs. Here's your own code:You shouldn't be using Longs at all if all you need is Integers, which is exactly why I asked those questions. Now that we actually know that you just need Integers then you can basically throw that VB6-esque code away and do this thr proper way. Presumably this code is in a form. At the top of the form, declare a variable of type Random, create an instance of the Random class and assign that instance to the variable, e.g.Code:Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd) + Low
End Function
Now, every time you need a random number, you simply call the Next method of that object, e.g.Code:'Create a random number generator.
Private rng As New Random
After that code the 'number' variable will contain in the range of 1 to 12 inclusive. Note that the minimum value passed to Next is inclusive while the maximum is exclusive, which is why you must pass 13 as the maximum to the method to be returned a maximum value of 12.Code:Dim number As Integer = rng.Next(1, 13)
Thanks, worked. Sorry im just not that good with vb.
That's fine but that makes it even more important to give us as much relevant information as you can, which includes answering questions when they're asked. If there's some legitimate reason that you can't provide the answer then that's fine too but if you post and don't even acknowledge the question then that comes across as your ignoring it.