|
-
May 6th, 2013, 05:00 PM
#1
Thread Starter
Lively Member
[RESOLVED] Confused on Random
I just noticed that my random numbers are NOT random. They always start at a certain number, and generate the same sequence every time my program runs anew. Here is an example of my code within the sub button_click:
Code:
Dim rando As String = CInt(Int((3 - 1 + 1) * Rnd() + 1))
If rando = 1 Then
For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
If ele.GetAttribute("id").ToLower = "RadioGroup0".ToLower Then
ele.InvokeMember("click")
End If
Next
End If
I use a different version of this for different elements, but the results are the same. Random is not random. On other forums, they say to put your Rnd outside the loop, which in this case it is, but it's still not random! Whatever "seed" was used when I made the program is always the same seed that is used whenever the program runs and I don't know how to change that. Help?
-
May 6th, 2013, 05:19 PM
#2
Addicted Member
Re: Confused on Random
You asked about generating random numbers on this thread and the proper way to do so was pointed out, which is by using the Random class. Don't use the legacy Rnd function.
-
May 6th, 2013, 05:28 PM
#3
Thread Starter
Lively Member
Re: Confused on Random
I understand that, and I do use that code for the another part of my project, however I just found it easier to use that code above when I had four different if's to check on. So basically you're telling me Rnd does not work properly in vb.net because it'd legacy so I have to use New Random instead? Okay, guess I'll have to convert them all. Oh well. Thanks.
-
May 6th, 2013, 05:35 PM
#4
Addicted Member
Re: Confused on Random
By the way you only need to create one instance of the Random class and call it's Next method to create another random number.
vb.net Code:
Public Class Form1 Dim rand As New Random Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Generate random number between 1 and 10 Dim randomNumber1 As Integer = rand.Next(1, 11) 'Generate random number between 50 and 100 Dim randomNumber2 As Integer = rand.Next(50, 101) 'Generate random number between 500 and 1000 Dim randomNumber3 As Integer = rand.Next(500, 1001) Label1.Text = "Random number1: " & randomNumber1.ToString & " Random number2: " & randomNumber2.ToString & _ " Random number3: " & randomNumber3.ToString End Sub End Class
-
May 6th, 2013, 05:37 PM
#5
Re: Confused on Random
No he's telling you that Rnd() does not work in the way you expect it to and never did, whatever version of Visual Basic you were using. This is because it is based on a list of pseudo random numbers that never changes. There are ways to change the place at which you start on the list but the order from that point is unchanging. The new Random object is an improvment on this which gives much less predictable results ... which for once is just the way you want it!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 6th, 2013, 05:55 PM
#6
Re: Confused on Random
The problem is that you either never called Randomize, or are calling it in the wrong place. The purpose of the Randomize function is to see the random number generator (typically with the current system time down to the second). If you don't call Randomize then you will be getting a canned set of numbers. If you call Randomize too often then you will get the exact same sequence of numbers over and over until the system time increases, at which point you will get a new sequence of numbers for the next second, and so on. For that reason, you MUST call Randomize before using Rnd, but you must be calling it only once per run of the program.
The Random object is better, as there is no need to call Randomize (which should do nothing, but seems to cause trouble, though I haven't looked too carefully at it). However, the same problem exists. If you create new Random objects more than once per second you will get the same sequence from them. Therefore, you want to create only one Random object at form scope and use that wherever you need a random number.
However, I'm also curious as to why you would find something like this:
CInt(Int((3 - 1 + 1) * Rnd() + 1))
easier than just calling the .Next method of the Random object in any circumstance?
My usual boring signature: Nothing
 
-
May 6th, 2013, 05:57 PM
#7
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 6th, 2013, 06:03 PM
#8
Re: Confused on Random
Yeah, I didn't catch that. It won't matter, though, as the compiler will turn it into 3.
My usual boring signature: Nothing
 
-
May 6th, 2013, 06:10 PM
#9
Thread Starter
Lively Member
Re: Confused on Random
I got the code off msdn: http://msdn.microsoft.com/en-us/library/398ax69y.aspx
I can't find the exact page I saw it from, but it basically said to add one to he max range, and subtract one from the min range to get a true random number.
-
May 6th, 2013, 06:11 PM
#10
Thread Starter
Lively Member
Re: Confused on Random
 Originally Posted by Shaggy Hiker
However, I'm also curious as to why you would find something like this:
CInt(Int((3 - 1 + 1) * Rnd() + 1))
easier than just calling the .Next method of the Random object in any circumstance?
Because I'm an idiot and didn't feel like recoding a few lines that were already coded because I was being lazy and didn't think it mattered until just right now when I realized, it does matter! Live and learn.... live and learn.
-
May 6th, 2013, 08:00 PM
#11
Re: [RESOLVED] Confused on Random
Funny how that works. We'd be so much better off if we just believed the wet paint sign and didn't have to touch 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
|