Results 1 to 9 of 9

Thread: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my Code.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Posts
    108

    Resolved [RESOLVED] [2008] How to pick a random number between X and Y Need help with my Code.

    I have this code to pick a random number Between X and Y
    X = Minimum Number
    Y = Maximum Number

    But the problem is i wanted to know if there is a better random function than the one i have.

    Because right now i put

    Min = 1
    Max= 2

    and it keeps picking 1

    if i had min =1 and max =2 i want it to pick 1 sometimes and 2 sometimes

    and now i tried min = 1 max = 3
    and it never picks 3, it only picks 1 and 2

    Code:
    ok Code:
    1. Public Class Form1
    2.     Public Function RandomNumber(ByVal MinNumber As Integer, ByVal MaxNumber As Integer) As Integer
    3.         Dim r As New Random(System.DateTime.Now.Millisecond)
    4.         Return Convert.ToInt32(r.Next(MinNumber, MaxNumber))
    5.     End Function
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.  
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         MsgBox(RandomNumber(1, 3))
    13.     End Sub
    14. End Class

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2008] How to pick a random number between X and Y Need help with my Code.

    The Random object returns a number that includes the lower bound, but does NOT include the upper bound. Therefore, if you want to include both min and max, you need to pick a random number from Min to Max+1
    My usual boring signature: Nothing

  3. #3
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [2008] How to pick a random number between X and Y Need help with my Code.

    You need to add +1 to your max number. If you didn't round your answer off you would see why.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Posts
    108

    Re: [2008] How to pick a random number between X and Y Need help with my Code.

    Thanks a lot guys +1 worked great.

  5. #5
    Member
    Join Date
    Apr 2009
    Posts
    57

    Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C

    i noticed if you use your code in a replace string it will bring up the same chr's... heres how to get a more random number

    vb Code:
    1. Public Function RandomNumber(ByVal Lowest As Double, ByVal Highest As Double) As Double
    2.         Dim i As Double
    3.  
    4.         Do : Application.DoEvents()
    5.             Randomize()
    6.             i = CDbl((Rnd() * Highest) + Lowest)
    7.         Loop Until i <= Highest
    8.         Return i
    9.     End Function

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C

    Quote Originally Posted by babyjesus View Post
    i noticed if you use your code in a replace string it will bring up the same chr's... heres how to get a more random number

    vb Code:
    1. Public Function RandomNumber(ByVal Lowest As Double, ByVal Highest As Double) As Double
    2.         Dim i As Double
    3.  
    4.         Do : Application.DoEvents()
    5.             Randomize()
    6.             i = CDbl((Rnd() * Highest) + Lowest)
    7.         Loop Until i <= Highest
    8.         Return i
    9.     End Function
    The problem with the original code is the fact that a new Random object is created each time a random number is required. As such, if you request multiple random numbers in quick succession the same number will be generated each time because the same implicit seed is used. The solution is definitely not to revert that old VB6-style code. The answer is to just create a single Random object and then to call its Next method multiple times. the RandomNumber method is pointless because you should simply be calling Next. Also, the Convert.ToInt32 is pointless because Next already returns an Int32. The code only needs to be:
    Code:
    Public Class Form1
    
        Private rng As New Random
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MsgBox(Me.rng.Next(1, 3 + 1).ToString())
        End Sub
    End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C

    Aside from that, putting the Randomize call into the inner loop will cause the same trouble that creating the Random object in the sub is causing. Every time that Randomize is called, it will re-seed the VB6-style random number generator with the current system time. The DoEvents slows things down, but not enough for the system time to change between calls to Randomize, so you will just get the same number over and over again until the time increments. Technically, you could use that loop to time the loop itself, but that's kind of silly.
    My usual boring signature: Nothing

  8. #8
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C

    I think it's time Shaggy Hiker tests how long it takes to brute force the generated 'random' if we know the week it was generated
    VB 2005, Win Xp Pro sp2

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [RESOLVED] [2008] How to pick a random number between X and Y Need help with my C

    I don't even understand the question??? But I think I agree with it.
    My usual boring signature: Nothing

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