1

I want to make a generator that will show results in text box , i want it to randomly choose 0 or 1 , how can i make that ?

Kode that works :

Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim r as New Random()
Dim n As Integer = r.Next(2)
TextBox1.Text = CStr(n) 

    End Sub
End Class
  • What does "between 0 and 1" mean? Do you mean "either 0 or 1 randomly"? Or "a value >= 0.0 and <= 1.0"? – Ken White Jan 10 '14 at 03:13
  • 1
    Then please [edit] your question and make it clear that you want to "randomly select either 0 or 1", so we know what you're asking. (And for future reference: We can't see your screen or read your mind, so you need to be **specific** when you ask your question. If we can't understand what you're asking, we can't help you find an answer.) – Ken White Jan 10 '14 at 03:15
  • You can see examples [here](http://stackoverflow.com/questions/18676/random-int-in-vb-net) – SomeNickName Jan 10 '14 at 03:17

2 Answers2

1

Try the following

Dim r as New Random()
Dim n As Integer = r.Next(2)
TextBox1.Text = CStr(n)
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

You can use the Random class included in the .NET Framework. It has an instance method on it called Next, which take in a min/max value. Here's an example:

Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim rng As Random = New Random()
    Dim randomNumber As Integer = rng.Next(0,2)
    ' Do what you want with randomNumber from here...
End Sub
End Class
avanek
  • 1,649
  • 12
  • 20
  • Yes i did try the edited solution , it just sends 0 :/ –  Jan 10 '14 at 03:30
  • Did you run it multiple times? `rng.Next(0,2)` is a random selection of 0 and 1, like flipping a coin. You could get zero quite a few times before a one shows up in the RNG. – avanek Jan 10 '14 at 13:40
  • Just for some clarification, the minimum value in Next is _inclusive_, but the maximum value is _exclusive_, meaning `rng.Next(0,2)` will only possibly return 0 or 1 – garthhh Oct 23 '18 at 22:41