Results 1 to 5 of 5

Thread: [RESOLVED] Random functions

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Resolved [RESOLVED] Random functions

    Recently I've been playing with the random function for both javascript and vb.net... (Just a simple die roller: fields for number of dice and number of sides)

    Ok so if I make it so it rolls 100 times or something to that affect the average roll is ALWAYS the same... That really makes me wonder how random these randomly generated numbers are... I eventually decided to just multiply by date.now.seconds and then do a mod operation which made the average different... But what the heck. What is in the random class (.net) and math.random function (javascript) that is making it average out to the same number everytime.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Random functions

    A properly random sample is evenly distributed across the range of possible values. As such, if the random generator is working properly, the mean value will be close to the median. (0.5, if you don't multiply the numbers generated.) The more random numbers that you sample, the closer it should be.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Re: Random functions

    Quote Originally Posted by penagate
    A properly random sample is evenly distributed across the range of possible values. As such, if the random generator is working properly, the mean value will be close to the median. (0.5, if you don't multiply the numbers generated.) The more random numbers that you sample, the closer it should be.
    Gotcha. That makes sense. I don't know why that hadn't occurred to me right off the bat. I seem to have lost my math touch... what with not being in school anymore and all...

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    25

    Re: [RESOLVED] Random functions

    Here is a Random function for vb.net that might help you.


    Code:
        'Random number function used for comments
        Public Function RandomNumber(ByVal MaxNumber As Integer, Optional ByVal MinNumber As Integer = 0) As Integer
    
            'initialize random number generator
            Dim r As New Random(System.DateTime.Now.Millisecond)
    
            'if passed incorrect arguments, swap them
            'can also throw exception or return 0
    
            If MinNumber > MaxNumber Then
                Dim t As Integer = MinNumber
                MinNumber = MaxNumber
                MaxNumber = t
            End If
    
            Return r.Next(MinNumber, MaxNumber)
        End Function

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [RESOLVED] Random functions

    If the average is exactly the same you might be using the same seed each time on accident, which generates the same numbers.

    The average of independent, identically distributed random variables approaches the normal distribution as the number of variables gets large. This is the central limit theorem, and a huge reason why the normal distribution is so important in statistics. The theorem gives the mean (variance) of the resulting normal distribution as the mean of the random variables (variance of those variables divided by the number of variables). This explains rigorously the behavior Penagate describes (though "median" should be replaced by "mean"; for instance, a distribution sampling uniformly from the numbers 0, 100, and 101 has median 100 but mean ~67, which I wouldn't call close at all).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

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