Getting even/odd numbers specifically with a random number generator in visual basic [duplicate]

StackOverflow https://stackoverflow.com/questions/23674195

  •  23-07-2023
  •  | 
  •  

Domanda

trying to make a random number generator using visual basic for a school project. The user would enter in 2 different values in textbox1 and textbox 2, press a button and a random number would be generated between these 2 digits (this random number would be displayed in textbox3). This was too basic for the project, so i decided to add in 2 checkboxs which, when checked would make the generated number either even or odd.

Really need some help with an algorithm that limits the random number to be even or odd. Any help is greatly appreciated! :) (checkbox1 is for making it even, checkbox2 for odd)

Dim answer As Integer
Dim result As Integer




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

    TextBox3.Clear()
    TextBox3.Text = answer

    If CheckBox1.Checked = False And CheckBox2.Checked = False Then
        answer = CInt(Int((TextBox2.Text * Rnd() + TextBox1.Text)))
    End If

^ the above code also seems to generate random numbers in a specific order, always starting from 0, any help with this would be greatly appreciated :)

    If CheckBox1.Checked = True Then
        Do Until result = 0
            result = CDec(TextBox1.Text / 2) - CInt(TextBox1.Text / 2)

        Loop
        If result = 0 Then
            answer = CInt(Int((TextBox2.Text * Rnd() + TextBox1.Text)))
        End If



    End If

End Sub
È stato utile?

Soluzione

This is what I'd do to solve this problem:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles Button1.Click

    'Parse the two numbers
    Dim minValue = Integer.Parse(TextBox1.Text)
    Dim maxValue = Integer.Parse(TextBox2.Text)

    'Create a list of all possible valid numbers
    Dim values = Enumerable.Range(minValue, maxValue - minValue).ToArray()

    'Keep only the even numbers if selected
    If CheckBox1.Checked Then
        values = values.Where(Function (v) v Mod 2 = 0).ToArray()
    End If

    'Keep only the odd numbers if selected
    If CheckBox2.Checked Then
        values = values.Where(Function (v) v Mod 2 = 1).ToArray()
    End If

    'Check there are numbers
    If values.Length = 0 Then
        TextBox3.Text = "There no available numbers to choose."
    Else
        '`rnd` here is `System.Random` as I didn't know what `Rnd()` was.
        TextBox3.Text = values(rnd.Next(0, values.Length)).ToString()
    End If

End Sub

Altri suggerimenti

You could use a function to generate either an even or odd number depending on which checkbox is checked, the functions would use mod to determine whether the generated number id even/odd. If it is not what you require, then it would try again until the generated number matches. For example:

  Private Sub btnGenerate_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerate.Click
    If chkOdd.Checked Then
        GenerateOdd()
    ElseIf chkEven.Checked Then
        GenerateEven()
    End If
End Sub

Private Function GenerateOdd()
    Dim r = CInt(Math.Ceiling(Rnd() * 100))
    If ((r Mod 2) = 0) Then
        'r is even, generate another number and try again
        GenerateOdd()
    Else
        'r is odd we have a match
        yourTextBox.Text = r
        Return r
    End If
    Return Nothing
End Function

Private Function GenerateEven()
    Dim r = CInt(Math.Ceiling(Rnd() * 100))
    If ((r Mod 2) = 0) Then
        'r is even, we have a match
        yourTextBox.Text = r
        Return r
    Else
        'r is odd, generate another number and try again
        GenerateEven()
    End If
    Return Nothing
End Function

I haven't tried this but you get the point!

*Edit - the (Rnd() * 100)) is a random number between 1 and 100

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top