What can I use to generate an Even random number please.
Printable View
What can I use to generate an Even random number please.
What you could do is generate a single dimensional array of random numbers. Then loop through the array using a For Next
loop but with a Step of 2.
Heres a link on Random
:)
Thanks RobDog, I didn't notice the thread was split before I posted again. I think that I got the Random() thing sussed.
Array??? what?
Just generate a normal random integer and multiply it by 2 to get an even number.
Could anyone please explain the Math.Round function.
I want to know if that function affects the output generated. My guess is I'm just being noobish :confused:VB Code:
Dim RandomNo As Random = New Random lblSomething.Text = Math.Round(RandomNo.Next(2, 12) * 2)
OK, thanks that helped. I guess I don't need it then ;)
With this code:
I get this error:VB Code:
[COLOR=Red]Do Until (CInt(lblDiv1.Text) * 2) > lblDiv2.Text[/COLOR] lblDiv1.Text = Math.Round(RandomNo.Next(1, 24) * 2) lblDiv2.Text = Math.Round(RandomNo.Next(1, 24) * 2) Loop
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Integer' is not valid.
So what is wrong. I don't see a problem there... :confused: Does CInt only work when there is an integer already in lblDiv1?
Hi
You are trying to compare a numeric with a string and then put a numeric into a string. Use
VB Code:
Do Until (CInt(lblDiv1.Text) * 2) > CInt(lblDiv2.Text) lblDiv1.Text = Math.Round(RandomNo.Next(1, 24) * 2).ToString lblDiv2.Text = Math.Round(RandomNo.Next(1, 24) * 2).ToString Loop
Doesn't work. Still shows the same error. the line I highlighted is where the program halts so I guess that is where the error is. I think it is happening because no integer is generated befor the CInt function is used, but thats me talking :D I will try something later, but in the meantime, I'm going to bed. Goodnight ;D
Before that Do...Loop, check to see if the textbox is empty. Perform the operation only if the textbox is NOT empty.
Wait, I'm going to sleep now. Thats what I was thinking. Which loop can I change it to so that it performs the check after the calculation? In Psuedocode that would be the REPEAT...UNTIL loop so what's the VB.nET equivalent?
I should have realized that one. :blush: from the Prime number contest. Similar logic.Quote:
Originally Posted by wossname
I seem to do things the hard way lately. :(
@martw
Try this:
VB Code:
If (lblDiv1.Text <> "") And (lblDiv2.Text <> "") Then Do Until (CInt(Val(lblDiv1.Text)) * 2) > CInt(Val(lblDiv2.Text)) lblDiv1.Text = Math.Round(RandomNo.Next(1, 24) * 2).ToString lblDiv2.Text = Math.Round(RandomNo.Next(1, 24) * 2).ToString Loop Else MessageBox.Show("Textbox is empty") End If
I put the 'Val()' function in there just in case the user entered a non-numeric character.
Npath
Hi martw,
You were testing the code suggestion by putting a non-numeric in the textbox???????????? :sick:
Oh, crack! I forgot to mention that this code is in the FormLoad event. Sorry. It is meant to generate a random number which the user will use after a timer event enabls an object on the form. Sorry Npath, but thanks for going through that. So basically, the nubers are generated on Form_Load and thats it. So thats why I guess I need to generate the numbers before they are checked. So back to my previous question, what is the the equivalent loop in VB.NET to REPEAT...UNTIL in psuedocode? Thanks.
VB Code:
Do Until [condition is true] ' do stuff here Loop or Do While [condition is false] ' do stuff here Loop
The Do Loop still checks the condition before the action is exceuted. I want it to do this:
You showed me:VB Code:
REPEAT [This Action] UNTIL [Condition is true]
VB Code:
WHILE [Condition is False/True] [This Action] END WHILE
@martw,
I am also used to 'Repeat Until' loops (I just started using VB.NET). Luckily, VB.NET has this:
VB Code:
Dim UserInput As String Do Console.WriteLine("Enter q to quit") UserInput = Console.ReadLine() Loop Until UserInput = "q"
Npath
Wha? Its that easy? Thats emabarrassing... Thanks Npath :D
You're welcome...