Results 1 to 5 of 5

Thread: [RESOLVED] How Can I generate a True Random 1 or 2

  1. #1

    Thread Starter
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Resolved [RESOLVED] How Can I generate a True Random 1 or 2

    Never mind it was working I just had some other code messing it up that I hadn't noticed.
    Last edited by Vexslasher; Oct 31st, 2015 at 03:46 PM.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: How Can I generate a True Random 1 or 2

    I am confused what is not working. Could you please explain again.

  3. #3
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    485

    Re: [RESOLVED] How Can I generate a True Random 1 or 2

    Hello
    Try this. Create a form1 with a button and cut and paste this code. Run the program and see the numbers generated.
    Notice the dim rnd as new random is not in the same place as the NewRandomNumber = rnd.Next(1, 3) is.
    Code:
    Public Class Form1
        Dim rnd As New Random
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim NewRandomNumber As Integer
            Dim astring As String = ""
            For x = 0 To 20
                NewRandomNumber = rnd.Next(1, 3)
                astring += NewRandomNumber.ToString
            Next
            MsgBox(astring)
        End Sub
    End Class
    Good Luck
    George

  4. #4

    Thread Starter
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: [RESOLVED] How Can I generate a True Random 1 or 2

    Oh that's why it worked I wasn't sure what fixed it. But it was because I had moved the "dim rnd as new random" to the very top of the project, so it was in a different block. I didn't know that was what fixed it, had just been tinkering with my code and noticed it was working after I started this post. Thanks for the help though now I know how it works again, I had forgotten.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: [RESOLVED] How Can I generate a True Random 1 or 2

    Please do not edit your post after submitting. a new random SHOULD be declared out of method scope.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private ReadOnly Rng As New Random
    4.  
    5.     Private Sub Foo()
    6.         MsgBox(String.Concat(Enumerable.Range(1, 20).Select(Function(n) Rng.Next(1, 3))))
    7.     End Sub
    8. End Class

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