|
-
Aug 19th, 2012, 09:55 PM
#1
Thread Starter
Hyperactive Member
Random Numbers The Same
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
-
Aug 19th, 2012, 10:27 PM
#2
Re: Random Numbers The Same
Would this suffice?
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
Edit - Also, try including the last low in the CInt conversion and see if that works for you:
Code:
Rand = CInt((High - Low + 1) * Rnd() + Low)
I noticed it looked almost like the example given in the MSDN's library on the Rnd() function, but not quite.
Last edited by dday9; Aug 19th, 2012 at 10:36 PM.
-
Aug 19th, 2012, 10:28 PM
#3
Re: Random Numbers The Same
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?
-
Aug 20th, 2012, 03:35 AM
#4
Re: Random Numbers The Same
 Originally Posted by dday9
Would this suffice?
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
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.
-
Aug 20th, 2012, 08:30 AM
#5
Thread Starter
Hyperactive Member
Re: Random Numbers The Same
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)))
-
Aug 20th, 2012, 08:33 AM
#6
Re: Random Numbers The Same
How come you can post back but you can't answer my questions? I'd kinda like to help but can't without information.
-
Aug 20th, 2012, 08:43 AM
#7
Thread Starter
Hyperactive Member
Re: Random Numbers The Same
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?
-
Aug 20th, 2012, 08:59 AM
#8
Re: Random Numbers The Same
 Originally Posted by sherlockturtle
Well I dont know if I executed it once and only once.
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.
 Originally Posted by sherlockturtle
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?
Um, you HAVE tried it with Longs. Here's your own code:
Code:
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd) + Low
End Function
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:
'Create a random number generator.
Private rng As New Random
Now, every time you need a random number, you simply call the Next method of that object, e.g.
Code:
Dim number As Integer = rng.Next(1, 13)
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.
Last edited by jmcilhinney; Aug 20th, 2012 at 09:08 AM.
-
Aug 20th, 2012, 09:07 AM
#9
Thread Starter
Hyperactive Member
Re: Random Numbers The Same
Thanks, worked. Sorry im just not that good with vb.
-
Aug 20th, 2012, 09:10 AM
#10
Re: Random Numbers The Same
 Originally Posted by sherlockturtle
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.
-
Aug 20th, 2012, 09:55 AM
#11
Re: Random Numbers The Same
 Originally Posted by Evil_Giraffe
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.
Thank you for catching that it would return the same number twice, I guess I just whipped it up in a couple of seconds with out fully thinking about it.
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
|
Click Here to Expand Forum to Full Width
|