Random number generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tigger
    New Member
    • May 2007
    • 37

    #1

    Random number generator

    Hi there,
    i'm using random number generator to generate random numbers between 40 to 50.

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

    mnRandomNumber = GetRandomNumber (40, 50)

    End Sub



    Private Function GetRandomNumber (ByVal vnMinimumNumber As Integer, ByVal vnMaximumNumber As Integer)
    GetRandomNumber = CInt(Int((vnMax imumNumber - vnMinimumNumber + 1) * Rnd() + vnMinimumNumber ))
    Me.Label1.Text = GetRandomNumber
    End Function


    However, i don't want to make the numbers occur too randomly. For example, number 40 will occur 90% of the time. Or occur at 60% of the time. Is that possible?
  • JonJacobs
    New Member
    • Aug 2007
    • 22

    #2
    Yes, it's possible, but not as simple and straightforward as just generating random numbers.

    One approach would be to create an array of byte (or integer, etc., but your numbers are small enough for byte) and fill it with your numbers in proportion to your desired frequency of occurrence. a(0) to a(9) might have 40. a(10) and a(11) 41, a(12) to a(16) 42, etc. or whatever whacky or sensible arrangement you want. Now, use your random number generator to pick a number from 0 to the max index. Do this in a loop. Return the value whose index was selected (say the index randomly generated was 13, then you would return 42 in the above example), but before returning a value, set the array contents at that index to 0. If your random number picks a place that has a 0 value, just loop and try again with another random number.

    There is the theoretical possibility that, when most of the array has been used, and just a few values are left, the random index might land on a 0 value so many times that there is a noticable delay, but I've never had that happen, and I've used the technique many times.

    When the array is used up, re-populate it.

    HTH

    Jon

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      That's a very creative approach you've hit on there, Jon. I love it!

      It seems to me though, there is no need ot "zero out" entries after using them. The proportion of entries which have a particular number would determine the probability of hitting it. Say your array has 100 entries. If 60 of them contain a given number, then you'll have a 60% chance of hitting that number when indexing randomly. In other words, that number should show up roughly 60% of the time. You shouldn't need to change anything as you go.

      Comment

      • JonJacobs
        New Member
        • Aug 2007
        • 22

        #4
        Originally posted by Killer42
        It seems to me though, there is no need ot "zero out" entries after using them. The proportion of entries which have a particular number would determine the probability of hitting it.
        Good point!

        All the situations I had to do something like this involved using up the values and stopping when they were all consumed. I should have thought a little beyond that.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by vatyk
          I am new, to this forum and also to VB.Net ...
          I'm going to split this message off to a new discussion thread. Click here to skip to that thread.

          Comment

          Working...