|
-
Mar 23rd, 2007, 03:53 PM
#1
Thread Starter
New Member
[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.
-
Mar 23rd, 2007, 04:22 PM
#2
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.
-
Mar 23rd, 2007, 04:36 PM
#3
Thread Starter
New Member
Re: Random functions
 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...
-
Dec 4th, 2010, 12:16 AM
#4
Junior Member
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
-
Dec 4th, 2010, 03:48 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|