-
I want to generate four random integers with a total sum of 100 like (12, 43, 18, 27) and then want to adjust the hight of four rectangle shapes in the same ratio i.e. 12:43:18:27.The above set of numbers is to be generated randomly to change the hight of rectangle shapes. What I actually want is to display a graph of 4 colums with random value in percentage.
Please help.
-
generate 4 numbers with RND function, with accuracy you choose, calculate their sum and divide it by 100 and you have the ratio which you divide each number with as a final. Use Int() function to truncate
-
kedaman,
Just wondering, if you are trying to get 4 integers which sum to 100, and you generated 4 random numbers where the following shows the % results:
number 1 : 23.3%
number 2 : 23.3%
number 3 : 24.3%
number 4 : 29.0%
------
100.0%
What would the 4 integer numbers be?
-
23,23,24 and 29 would sum up 99, if you want to generate true random (that is equal chance for each number to have any value proportional to each other) then regenerate until you reach 100. Otherways you could add up the rest to a randomly choosed value.
-
How 'bout this:
Code:
Dim A, B, C, D
A = Int(Rnd * 100)
B = Int(Rnd * 100)
Do
If A + B > 100 Then
B = Int(Rnd * 100)
End If
Loop Until A + B =< 100
C = Int(Rnd * 100)
Do
If A + B + C > 100 Then
C = Int(Rnd * 100)
End If
Loop Until A + B + C =< 100
D = Int(Rnd * 100)
Do
If A + B + C + D > 100 Then
D = Int(Rnd * 100)
End If
Loop Until A + B + C + D =< 100
And there you have it!!
-
thats definitely biased to give higher values for A and B
-
Try this:
Code:
Dim Total, Temp, Val(1 to 4), CurVal
Total = 100
Do Until Total=0
Temp = Int(Rnd*1)
CurVal = CurVal + 1
If CurVal > 4 Then Curval = 1
Val(CurVal) = Val(CurVal) + Temp
Total = Total - Temp
Loop
Heh, hope that works :rolleyes:
-
biased to give values close to 25
-
Ok, ok: this one should work :rolleyes:
Code:
Dim Total, Temp, Val(1 to 4), CurVal
Total = 100
Do Until Total <= 0
Temp = Int(Rnd*25)
If Temp > Total Then Temp = Total
CurVal = CurVal + 1
If CurVal > 4 Then Curval = 1
Val(CurVal) = Val(CurVal) + Temp
Total = Total - Temp
Loop
Try it out. It generates completely random numbers :D
-
Give up, and save yourself some time :p
-
Thanks for the help extended to me. I succeeded in doing it throgh the following code.
Randomize
Cls
JA0 = Int((101 * Rnd) + 1)
JA1 = Int((101 * Rnd) + 1)
JA2 = Int((101 * Rnd) + 1)
JA3 = Int((101 * Rnd) + 1)
JT = JA0 + JA1 + JA2 + JA3
JS0 = Int((JA0 / JT) * 100)
JS1 = Int((JA1 / JT) * 100)
JS2 = Int((JA2 / JT) * 100)
JS3 = Int((JA3 / JT) * 100)
JH0 = Int((JS0 * 120 * 31) / 100)
JH1 = Int((JS1 * 120 * 31) / 100)
JH2 = Int((JS2 * 120 * 31) / 100)
JH3 = Int((JS3 * 120 * 31) / 100)
FrmPubopn!Shape1.Top = 3720 - JH0
FrmPubopn!Shape2.Top = 3720 - JH1
FrmPubopn!Shape3.Top = 3720 - JH2
FrmPubopn!Shape4.Top = 3720 - JH3
FrmPubopn!Shape1.Height = JH0
FrmPubopn!Shape2.Height = JH1
FrmPubopn!Shape3.Height = JH2
FrmPubopn!Shape4.Height = JH3
FrmPubopn.Show
End Sub
What I have done-
Generate 4 random nos range 0 to 100.
get their sum.
Devide each no by the sum and multiply by 100 to get percentage.