[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.
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
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).