|
-
Feb 4th, 2009, 09:10 PM
#1
Thread Starter
Lively Member
[RESOLVED] [2008] How to pick a random number between X and Y Need help with my Code.
I have this code to pick a random number Between X and Y
X = Minimum Number
Y = Maximum Number
But the problem is i wanted to know if there is a better random function than the one i have.
Because right now i put
Min = 1
Max= 2
and it keeps picking 1
if i had min =1 and max =2 i want it to pick 1 sometimes and 2 sometimes
and now i tried min = 1 max = 3
and it never picks 3, it only picks 1 and 2
Code:
ok Code:
Public Class Form1
Public Function RandomNumber(ByVal MinNumber As Integer, ByVal MaxNumber As Integer) As Integer
Dim r As New Random(System.DateTime.Now.Millisecond)
Return Convert.ToInt32(r.Next(MinNumber, MaxNumber))
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(RandomNumber(1, 3))
End Sub
End Class
-
Feb 4th, 2009, 09:39 PM
#2
Re: [2008] How to pick a random number between X and Y Need help with my Code.
The Random object returns a number that includes the lower bound, but does NOT include the upper bound. Therefore, if you want to include both min and max, you need to pick a random number from Min to Max+1
My usual boring signature: Nothing
 
-
Feb 4th, 2009, 09:40 PM
#3
Re: [2008] How to pick a random number between X and Y Need help with my Code.
You need to add +1 to your max number. If you didn't round your answer off you would see why.
-
Feb 4th, 2009, 09:47 PM
#4
Thread Starter
Lively Member
Re: [2008] How to pick a random number between X and Y Need help with my Code.
Thanks a lot guys +1 worked great.
-
Jul 10th, 2009, 06:47 AM
#5
Member
Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C
i noticed if you use your code in a replace string it will bring up the same chr's... heres how to get a more random number
vb Code:
Public Function RandomNumber(ByVal Lowest As Double, ByVal Highest As Double) As Double Dim i As Double Do : Application.DoEvents() Randomize() i = CDbl((Rnd() * Highest) + Lowest) Loop Until i <= Highest Return i End Function
-
Jul 10th, 2009, 07:18 AM
#6
Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C
 Originally Posted by babyjesus
i noticed if you use your code in a replace string it will bring up the same chr's... heres how to get a more random number
vb Code:
Public Function RandomNumber(ByVal Lowest As Double, ByVal Highest As Double) As Double
Dim i As Double
Do : Application.DoEvents()
Randomize()
i = CDbl((Rnd() * Highest) + Lowest)
Loop Until i <= Highest
Return i
End Function
The problem with the original code is the fact that a new Random object is created each time a random number is required. As such, if you request multiple random numbers in quick succession the same number will be generated each time because the same implicit seed is used. The solution is definitely not to revert that old VB6-style code. The answer is to just create a single Random object and then to call its Next method multiple times. the RandomNumber method is pointless because you should simply be calling Next. Also, the Convert.ToInt32 is pointless because Next already returns an Int32. The code only needs to be:
Code:
Public Class Form1
Private rng As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Me.rng.Next(1, 3 + 1).ToString())
End Sub
End Class
-
Jul 10th, 2009, 08:44 AM
#7
Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C
Aside from that, putting the Randomize call into the inner loop will cause the same trouble that creating the Random object in the sub is causing. Every time that Randomize is called, it will re-seed the VB6-style random number generator with the current system time. The DoEvents slows things down, but not enough for the system time to change between calls to Randomize, so you will just get the same number over and over again until the time increments. Technically, you could use that loop to time the loop itself, but that's kind of silly.
My usual boring signature: Nothing
 
-
Jul 10th, 2009, 09:13 AM
#8
Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C
I think it's time Shaggy Hiker tests how long it takes to brute force the generated 'random' if we know the week it was generated
-
Jul 10th, 2009, 01:10 PM
#9
Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C
I don't even understand the question??? But I think I agree with it.
My usual boring signature: Nothing
 
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
|