Results 1 to 11 of 11

Thread: [RESOLVED] Confused on Random

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Resolved [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?

  2. #2
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    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.

  4. #4
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    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:
    1. Public Class Form1
    2.  
    3.     Dim rand As New Random
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.  
    7.         'Generate random number between 1 and 10
    8.         Dim randomNumber1 As Integer = rand.Next(1, 11)
    9.         'Generate random number between 50 and 100
    10.         Dim randomNumber2 As Integer = rand.Next(50, 101)
    11.         'Generate random number between 500 and 1000
    12.         Dim randomNumber3 As Integer = rand.Next(500, 1001)
    13.  
    14.         Label1.Text = "Random number1: " & randomNumber1.ToString & " Random number2: " & randomNumber2.ToString & _
    15.                       " Random number3: " & randomNumber3.ToString
    16.  
    17.     End Sub
    18. End Class

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Confused on Random

    (3 - 1 + 1)
    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!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Confused on Random

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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
  •  



Click Here to Expand Forum to Full Width