How to generate random numbers

2019-06-03 22:17发布

问题:

i'm new to vb and i have 3 textboxs that i want to set it to a random value when the user click a specific button in in the form so :
here's the code :

Private Sub cmdjouer_Click(Index As Integer)
  txt1 = Math.Randomize(9)
 txt2 = Math.Randomize(9)
 txt3 = Math.Randomize(9)
End Sub

i get the following error expected function or variable

i'm sure that the problom is with the Randomize function.

Any idea will be appreciated

回答1:

You are confusing VB.NET with VB6. They are drastically different from each other. In the future, when you look for examples, documentation, and help online, be sure to specify VB6 to ensure that you are dealing with the correct language. They are essentially two completely different languages.

VB6

In VB6, you need to initially seed the random number generator using the Randomize function. Then, to generate a random number, you must use the Rnd function, for instance:

Private Sub cmdjouer_Click(Index As Integer)
    Randomize()
    txt1.Text = Int((Rnd * 9) + 1)
    txt2.Text = Int((Rnd * 9) + 1)
    txt3.Text = Int((Rnd * 9) + 1)
End Sub

VB.NET

Math.Randomize is a method in the Smart Personal Objects Technology (SPOT) namespace. I doubt that is what you are actually looking for. You probably just want to use the System.Random class, like this:

Private Sub cmdjouer_Click(sender As Object, e As EventArgs) Handles cmdjouer.Click
    Dim r As New Random()
    txt1.Text = r.Next(9).ToString()
    txt2.Text = r.Next(9).ToString()
    txt3.Text = r.Next(9).ToString()
End Sub

As others have pointed out, you don't want to set your text box reference variable to the number (e.g. txt1 = ...). You need to set the Text property of the text box.

Also, as was pointed out in the comments below, I called Randomize or created the New Random object inside the button's Click event. I did so to simplify the example, but in actuality, that would be bad practice. In either case, the seeding of the random number generator should, ideally, only happen once, typically when the application starts. By re-seeding the generator each time, it can cause the results to be less random.



回答2:

You should read the documentation.

Microsoft.SPOT.Math.Randomize says:

Sets a random starting point for random number generation.

So it doesn’t generate random numbers, it initialises the generator. You want Microsoft.SPOT.Math.Random. But better yet, use the superior Random class to generate random numbers.

The code has more problems, however. As Constanta mentioned in a comment, txt1 etc. are probably text boxes so you need to change your assignments to say txt1.Text = … etc. However, that is still bad code and won’t compile with Option Strict On. Which should always be On. So you need to convert manually, leaving you with this code:

Dim rng As New Random()
txt1.Text = rng.Next(9).ToString()
txt2.Text = rng.Next(9).ToString()
txt3.Text = rng.Next(9).ToString()


回答3:

Try this

' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 6.
Dim value As Integer = CInt(Int((6 * Rnd()) + 1)) txt1.text = cstr(value)


标签: vb6