Rnd function question

MCAdude

Registered User.
Local time
Today, 15:35
Joined
Mar 4, 2002
Messages
13
I've got the following (simple) code put under a button

Private Sub random_Click()
Dim MyValue
Me![numbera] = 0
Me![randomdate] = Date
Me![randomtime] = Time()
MyValue = Int((2 * Rnd) + 1) ' Generate random value between 1 and 2.
Me![numbera] = MyValue

It works like it should, but the problem is that when I clicked it 120 times the distribution was 70 to 50. Is there a way to make sure that the distribution of the number is max 45 to 55 when dealing with about 100 randomized records?

Thanks M.
 
If I understand correctly what you have is the user pressing a button to generate a 1 0r a 2 in the field [numbera], the user then moves to a new record and presses the button again and so on to populate the table.

What you want is that in any population the number '1' only occurs in 45% to 55% of records? - which I guess means it isn't then strictly random anymore.

Try this:
Use the DCOUNT function to count the total number of records in the table and also the total number with [numbera]=1, divide one by 'tother to get the percentage and then use this to decide if you need to override the random number generation to get back within the range you want. One problem is that for the second record of the population you will, by definition, have a current distribution of 0%/100% or 100%/0% so you want a sizable number of records first before your override kicks-in.

Here's an example:

-----
Dim MyValue As Variant
Dim Total_num As Integer
Dim No_ones As Integer
Dim Percent_ones As Integer

Total_num = DCount("*", "{YourTableName}")
No_ones = DCount("*", "{YourTableName}", "[numbera] = 1")

Me![randomdate] = Date
Me![randomtime] = Time()

'don't override random until population is large enough
If Total_num > 10 Then

'calculate the percentage
Percent_ones = (No_ones / Total_num) * 100

Select Case Percent_ones

'If less than 45% 1's force a '1'
Case Is < 45
Me![numbera] = 1
'If more than 55% 1's force a '2'
Case Is > 55
Me![numbera] = 2
'Otherwise Generate random value between 1 and 2.
Case Else
MyValue = Int((2 * Rnd) + 1)
Me![numbera] = MyValue
End Select
Else
'Generate random value between 1 and 2.
MyValue = Int((2 * Rnd) + 1)
Me![numbera] = MyValue
End If

Hope this was what you were looking for.

[This message has been edited by Dembrey (edited 03-07-2002).]
 
If you want all random numbers between 45 and 55:
Int((55 - 45 +1) * Rnd + 45)

Or more generally
Int((iMax - iMin +1) * Rnd + iMin)

Alex

[This message has been edited by Alexandre (edited 03-07-2002).]
 
Humm yeah, much more succinct. Only goes to show why come here more FOR help than TO help.
 
No Dembrey, your code was the one I was looking for. So really, thanks for the help
 

Users who are viewing this thread

Back
Top Bottom