Results 1 to 21 of 21

Thread: Generate an Even random number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Generate an Even random number

    What can I use to generate an Even random number please.
    Last edited by RobDog888; May 13th, 2005 at 11:26 PM. Reason: Split thread.
    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!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Generate an Even random number

    Thanks RobDog, I didn't notice the thread was split before I posted again. I think that I got the Random() thing sussed.
    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!

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Unhappy Re: Generate an Even random number

    Could anyone please explain the Math.Round function.
    VB Code:
    1. Dim RandomNo As Random = New Random
    2.  
    3. 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!

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Generate an Even random number


  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Generate an Even random number

    OK, thanks that helped. I guess I don't need it then
    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!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Unhappy Re: Generate an Even random number

    With this code:

    VB Code:
    1. [COLOR=Red]Do Until (CInt(lblDiv1.Text) * 2) > lblDiv2.Text[/COLOR]
    2.             lblDiv1.Text = Math.Round(RandomNo.Next(1, 24) * 2)
    3.             lblDiv2.Text = Math.Round(RandomNo.Next(1, 24) * 2)
    4.         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!

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    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:
    1. Do Until (CInt(lblDiv1.Text) * 2) > CInt(lblDiv2.Text)
    2.             lblDiv1.Text = Math.Round(RandomNo.Next(1, 24) * 2).ToString
    3.             lblDiv2.Text = Math.Round(RandomNo.Next(1, 24) * 2).ToString
    4.         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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Generate an Even random number

    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 I will try something later, but in the meantime, I'm going to bed. Goodnight ;D
    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!

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Generate an Even random number

    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 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!

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Generate an Even random number

    Quote 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  14. #14
    New Member
    Join Date
    May 2005
    Posts
    11

    Re: Generate an Even random number

    @martw

    Try this:

    VB Code:
    1. If (lblDiv1.Text <> "") And (lblDiv2.Text <> "") Then
    2.             Do Until (CInt(Val(lblDiv1.Text)) * 2) > CInt(Val(lblDiv2.Text))
    3.                 lblDiv1.Text = Math.Round(RandomNo.Next(1, 24) * 2).ToString
    4.                 lblDiv2.Text = Math.Round(RandomNo.Next(1, 24) * 2).ToString
    5.             Loop
    6.         Else
    7.             MessageBox.Show("Textbox is empty")
    8.         End If

    I put the 'Val()' function in there just in case the user entered a non-numeric character.

    Npath

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    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.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Generate an Even random number

    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.
    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!

  17. #17
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265

    Re: Generate an Even random number

    VB Code:
    1. Do Until [condition is true]
    2. ' do stuff here
    3. Loop
    4.  
    5.  
    6. or
    7.  
    8. Do While [condition is false]
    9. ' do stuff here
    10. Loop
    Nick.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    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:
    1. REPEAT
    2. [This Action]
    3. UNTIL [Condition is true]
    You showed me:
    VB Code:
    1. WHILE [Condition is False/True]
    2. [This Action]
    3. 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!

  19. #19
    New Member
    Join Date
    May 2005
    Posts
    11

    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:
    1. Dim UserInput As String
    2.         Do
    3.             Console.WriteLine("Enter q to quit")
    4.             UserInput = Console.ReadLine()
    5.         Loop Until UserInput = "q"

    Npath

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Generate an Even random number

    Wha? Its that easy? Thats emabarrassing... Thanks Npath
    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!

  21. #21
    New Member
    Join Date
    May 2005
    Posts
    11

    Re: Generate an Even random number

    You're welcome...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width