none
Vb.net Rnd() function doesn't work

    Question

  • I am trying to make a random dice game. So I have typed in: "Dim value As Integer = CInt(Int((6 * Rnd()) + 1))" to make a random number, but it converts the "Rnd()" to "RND()". It then doesn't work because it says "RND()" is a namespace and won't let the code run. Can anyone help?
    Sunday, April 06, 2014 9:25 PM

Answers

  • Hi,

     You should be using the Random class to generate random numbers in VB.Net. Check out the msdn link bellow. There are examples of using it there also.   :)

    Random Class

    Sunday, April 06, 2014 9:32 PM
  • As Iron said check out the Random Class

        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            For x As Integer = 1 To 10
                'roll one die 10 times
                Debug.WriteLine(RollAdie)
            Next
        End Sub
    
        Dim prng As New Random
    
        Private Function RollAdie() As Integer
            Return prng.Next(1, 7)
        End Function
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Sunday, April 06, 2014 10:03 PM
  • Below is code for a technique I originally found from someones VB code for shuffling cards. I don't have the link to it. But it seems to work better than using the Microsoft Random number generator in my opinion.

    Also anything you want to know about the functions in it can be researched using the Microsoft Development Network (MSDN) Library search engine.

    Also you can see right above and below the code at this link Random Class the sentences "The following example illustrates that two Random objects that are instantiated in close succession generate an identical series of random numbers. This problem can be avoided by creating a single Random object rather than multiple ones." which lead me to believe that the Random class isn't really random at all.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Die1 = Enumerable.Range(1, 6)
            Dim RollDie1 = Die1.OrderBy(Function(a) Guid.NewGuid())
            Label6.Text = RollDie1(0).ToString
            Dim Die2 = Enumerable.Range(1, 6)
            Dim RollDie2 = Die2.OrderBy(Function(a) Guid.NewGuid())
            Label7.Text = RollDie2(0).ToString
            Dim Die3 = Enumerable.Range(1, 6)
            Dim RollDie3 = Die3.OrderBy(Function(a) Guid.NewGuid())
            Label8.Text = RollDie3(0).ToString
            Dim Die4 = Enumerable.Range(1, 6)
            Dim RollDie4 = Die4.OrderBy(Function(a) Guid.NewGuid())
            Label9.Text = RollDie4(0).ToString
            Dim Die5 = Enumerable.Range(1, 6)
            Dim RollDie5 = Die5.OrderBy(Function(a) Guid.NewGuid())
            Label10.Text = RollDie5(0).ToString
        End Sub
    
    End Class


    Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.


    Monday, April 07, 2014 12:55 AM

All replies

  • Hi,

     You should be using the Random class to generate random numbers in VB.Net. Check out the msdn link bellow. There are examples of using it there also.   :)

    Random Class

    Sunday, April 06, 2014 9:32 PM
  • As Iron said check out the Random Class

        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            For x As Integer = 1 To 10
                'roll one die 10 times
                Debug.WriteLine(RollAdie)
            Next
        End Sub
    
        Dim prng As New Random
    
        Private Function RollAdie() As Integer
            Return prng.Next(1, 7)
        End Function
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Sunday, April 06, 2014 10:03 PM
  • Below is code for a technique I originally found from someones VB code for shuffling cards. I don't have the link to it. But it seems to work better than using the Microsoft Random number generator in my opinion.

    Also anything you want to know about the functions in it can be researched using the Microsoft Development Network (MSDN) Library search engine.

    Also you can see right above and below the code at this link Random Class the sentences "The following example illustrates that two Random objects that are instantiated in close succession generate an identical series of random numbers. This problem can be avoided by creating a single Random object rather than multiple ones." which lead me to believe that the Random class isn't really random at all.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Die1 = Enumerable.Range(1, 6)
            Dim RollDie1 = Die1.OrderBy(Function(a) Guid.NewGuid())
            Label6.Text = RollDie1(0).ToString
            Dim Die2 = Enumerable.Range(1, 6)
            Dim RollDie2 = Die2.OrderBy(Function(a) Guid.NewGuid())
            Label7.Text = RollDie2(0).ToString
            Dim Die3 = Enumerable.Range(1, 6)
            Dim RollDie3 = Die3.OrderBy(Function(a) Guid.NewGuid())
            Label8.Text = RollDie3(0).ToString
            Dim Die4 = Enumerable.Range(1, 6)
            Dim RollDie4 = Die4.OrderBy(Function(a) Guid.NewGuid())
            Label9.Text = RollDie4(0).ToString
            Dim Die5 = Enumerable.Range(1, 6)
            Dim RollDie5 = Die5.OrderBy(Function(a) Guid.NewGuid())
            Label10.Text = RollDie5(0).ToString
        End Sub
    
    End Class


    Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.


    Monday, April 07, 2014 12:55 AM