|
-
May 13th, 2005, 11:23 PM
#1
Thread Starter
Lively Member
-
May 13th, 2005, 11:32 PM
#2
Re: Generate an Even random number
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 14th, 2005, 02:57 AM
#3
Thread Starter
Lively Member
-
May 14th, 2005, 03:59 AM
#4
Re: Generate an Even random number
Array??? what?
Just generate a normal random integer and multiply it by 2 to get an even number.
I don't live here any more.
-
May 14th, 2005, 04:13 AM
#5
Thread Starter
Lively Member
Re: Generate an Even random number
Could anyone please explain the Math.Round function.
VB Code:
Dim RandomNo As Random = New Random
lblSomething.Text = Math.Round(RandomNo.Next(2, 12) * 2)
I want to know if that function affects the output generated. My guess is I'm just being noobish
I never know what to put in this section...

So sue me... ... ... I'm just kidding...
www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!
-
May 14th, 2005, 04:40 AM
#6
Re: Generate an Even random number
-
May 14th, 2005, 04:57 AM
#7
Thread Starter
Lively Member
-
May 14th, 2005, 07:37 AM
#8
Thread Starter
Lively Member
Re: Generate an Even random number
With this code:
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
I get this error:
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... Does CInt only work when there is an integer already in lblDiv1?
I never know what to put in this section...

So sue me... ... ... I'm just kidding...
www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!
-
May 14th, 2005, 07:42 AM
#9
PowerPoster
Re: Generate an Even random number
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
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 14th, 2005, 07:57 AM
#10
Thread Starter
Lively Member
-
May 14th, 2005, 08:04 AM
#11
Re: Generate an Even random number
Before that Do...Loop, check to see if the textbox is empty. Perform the operation only if the textbox is NOT empty.
-
May 14th, 2005, 08:13 AM
#12
Thread Starter
Lively Member
-
May 14th, 2005, 08:27 AM
#13
Re: Generate an Even random number
 Originally Posted by wossname
Array??? what?
Just generate a normal random integer and multiply it by 2 to get an even number.
I should have realized that one. from the Prime number contest. Similar logic.
I seem to do things the hard way lately.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 14th, 2005, 04:13 PM
#14
New Member
Re: Generate an Even random number
@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
-
May 14th, 2005, 06:41 PM
#15
PowerPoster
Re: Generate an Even random number
Hi martw,
You were testing the code suggestion by putting a non-numeric in the textbox????????????
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 14th, 2005, 07:10 PM
#16
Thread Starter
Lively Member
-
May 14th, 2005, 08:06 PM
#17
PowerPoster
Re: Generate an Even random number
VB Code:
Do Until [condition is true]
' do stuff here
Loop
or
Do While [condition is false]
' do stuff here
Loop
-
May 14th, 2005, 08:52 PM
#18
Thread Starter
Lively Member
Re: Generate an Even random number
The Do Loop still checks the condition before the action is exceuted. I want it to do this:
VB Code:
REPEAT
[This Action]
UNTIL [Condition is true]
You showed me:
VB Code:
WHILE [Condition is False/True]
[This Action]
END WHILE
I never know what to put in this section...

So sue me... ... ... I'm just kidding...
www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!
-
May 14th, 2005, 11:27 PM
#19
New Member
Re: Generate an Even random number
@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
-
May 14th, 2005, 11:30 PM
#20
Thread Starter
Lively Member
-
May 15th, 2005, 12:09 AM
#21
New Member
Re: Generate an Even random number
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|