so i made a random number generator where you put down 2 values in text boxes and it generates a number between them (when you hit cmdGenerate)
then I wanted to add options for evens only and odds only, but I don't know how. here's what I have, I just need to know how to make it so the generated # is even for the first case, and odd for the second case
Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
Select Case True
Case optEven.Checked
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text)
Case optOdd.Checked
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text)
Case Else
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text)
End Select
Else
MsgBox("Not a number, idiot.", MsgBoxStyle.Exclamation, "Error")
End If
End Sub
thanks
conure says: or i could jsut incase my shoes in papar mache, followed by my dog
|||Lethargy||| Mr. Mohawk|||
|||feel free to contact me via PMs, AIM, MSNM, or Xfire if you have any questions|||
xbox7887 wrote:For an even case, do a logical AND of the number by 0xFFFFFFFE and for an odd case do a logical OR by 1.
thanks for helping me, but can you be a bit more specific? I'm new to programming. Are you saying I need to do an And/Or statement?
conure says: or i could jsut incase my shoes in papar mache, followed by my dog
|||Lethargy||| Mr. Mohawk|||
|||feel free to contact me via PMs, AIM, MSNM, or Xfire if you have any questions|||
int number = Random().Next();
int evenNumber = number AND 0xFFFFFFFE;
int oddNumber = number OR 1;
Also when referencing hexidecimal, they may use a different format like 0FFFFFFFEH...you should be able to figure it out from there. And pay no attention to my C-style variable declarations, use the Dim bullshit that VB makes you do
Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
Select Case True
Case optEven.Checked
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text) And Integer.Parse("FFFFFFFE", Globalization.NumberStyles.HexNumber)
Case optOdd.Checked
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text) Or 1
Case Else
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text)
End Select
Else
MsgBox("Not a number, idiot.", MsgBoxStyle.Exclamation, "Error")
End If
End Sub
Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
Select Case True
Case optEven.Checked
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text) And Integer.Parse("FFFFFFFE", Globalization.NumberStyles.HexNumber)
Case optOdd.Checked
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text) Or 1
Case Else
Dim objRandom As System.Random
objRandom = New Random(Now.Millisecond)
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text)
End Select
Else
MsgBox("Not a number, idiot.", MsgBoxStyle.Exclamation, "Error")
End If
End Sub
that worked, thanks!
conure says: or i could jsut incase my shoes in papar mache, followed by my dog
|||Lethargy||| Mr. Mohawk|||
|||feel free to contact me via PMs, AIM, MSNM, or Xfire if you have any questions|||
Seeing as the 'Random' class is always used you might as well declare that at the start, and you might as well make the hex conversion more "vb"-ish too.
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
Dim objRandom = New Random()
Select Case True
Case optEven.Checked
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text) And Val("&HFFFFFFFE")
Case optOdd.Checked
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text) Or 1
Case Else
lblRandomNumber.Text = objRandom.Next(TextBox1.Text, TextBox2.Text)
End Select
Else
MsgBox("Not a number, idiot.", MsgBoxStyle.Exclamation, "Error")
End If
Thanks prey. One small problem, I was playing with the program and I entered 3 and 5, then checked the evens box. It spit out 4s and 2s
does anyone know why this is? or possible solutions?
and yes, it does it every time, I closed the app and tried again with the same results
(yes the evens box is checked, but it's under more options
conure says: or i could jsut incase my shoes in papar mache, followed by my dog
|||Lethargy||| Mr. Mohawk|||
|||feel free to contact me via PMs, AIM, MSNM, or Xfire if you have any questions|||
Because you're still running your even odd code...if it randomly generates a 3 (which is your min), then the even code will lop off the ones bit and make it a two...
Basically, you get a random number...if you want it odd the code rounds up, if you want it even the code rounds down ;p
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
'first we run a few tests to make sure there is at least a gap of two
Dim varDiff As Integer = TextBox2.Text - TextBox1.Text
If varDiff = 0 Or varDiff = 1 Or varDiff = -1 Then End
'If we're still here then yes there is an 'in-between'. Without those
'tests the while loops could have potentially looped on forever
'Declare
Dim objRandom = New Random()
Dim varRandom As Integer = TextBox1.Text - 1
'Get random integer
Select Case True
Case optEven.Checked
'If the var is smaller or equal to textbox1's text, or the var
'is bigger than or equal to textbox2's text; continue looping
While varRandom <= TextBox1.Text Or varRandom >= TextBox2.Text
varRandom = objRandom.Next(TextBox1.Text, TextBox2.Text) And Val("&HFFFFFFFE")
End While
Case optOdd.Checked
While varRandom <= TextBox1.Text Or varRandom >= TextBox2.Text
varRandom = objRandom.Next(TextBox1.Text, TextBox2.Text) Or 1
End While
Case Else
varRandom = objRandom.Next(TextBox1.Text, TextBox2.Text)
End Select
'Display
lblRandomNumber.Text = varRandom
Else
'Error handling
lblRandomNumber.Text = "Not a number, idiot."
End If
Private Function IsEven(byval number As Integer)As Boolean
int rem = number mod 2 ' mod returns the remainder of division
return rem = 0 ' if it's remainder is 0 return true, else false
End Function
Wow, you guys are really helpful!
Here is the solution I came up with, it fixes the problem fine, but let me know if this is bad practice or something. All I did was add this:
If CInt(lblRandomNumber.Text) < CInt(TextBox1.Text) Then
cmdGenerate.PerformClick()
End If
That way if the number isn't equal to or above the min value, it tries again, and so on, until it generates a proper number
conure says: or i could jsut incase my shoes in papar mache, followed by my dog
|||Lethargy||| Mr. Mohawk|||
|||feel free to contact me via PMs, AIM, MSNM, or Xfire if you have any questions|||
Patrickh wrote:Wow, you guys are really helpful!
Here is the solution I came up with, it fixes the problem fine, but let me know if this is bad practice or something. All I did was add this:
If CInt(lblRandomNumber.Text) < CInt(TextBox1.Text) Then
cmdGenerate.PerformClick()
End If
That way if the number isn't equal to or above the min value, it tries again, and so on, until it generates a proper number
Yea ok, but that doesn't take into account if the number is bigger than TextBox2. Also you shouldn't have to 'CInt', vB should take care of that for you. One last thing, is that code in the 'cmdGenerate.PerformClick()' method? Because otherwise the number returned could again be outside the boundaries and you wouldn't have any to code left to check again.
Patrickh wrote:Wow, you guys are really helpful!
Here is the solution I came up with, it fixes the problem fine, but let me know if this is bad practice or something. All I did was add this:
If CInt(lblRandomNumber.Text) < CInt(TextBox1.Text) Then
cmdGenerate.PerformClick()
End If
That way if the number isn't equal to or above the min value, it tries again, and so on, until it generates a proper number
Yea ok, but that doesn't take into account if the number is bigger than TextBox2. Also you shouldn't have to 'CInt', vB should take care of that for you. One last thing, is that code in the 'cmdGenerate.PerformClick()' method? Because otherwise the number returned could again be outside the boundaries and you wouldn't have any to code left to check again.
Yes it is the last thing inside the click event, so if the number is less than the min it just tries again. as for the greater than textbox2, now that i think about it i should add that, because if you asked for an odd number between 2 and 4 it would give you five. good call
conure says: or i could jsut incase my shoes in papar mache, followed by my dog
|||Lethargy||| Mr. Mohawk|||
|||feel free to contact me via PMs, AIM, MSNM, or Xfire if you have any questions|||