Click to See Complete Forum and Search --> : Random shape height
uttamsaxena
Apr 21st, 2001, 02:57 PM
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.
kedaman
Apr 21st, 2001, 03:09 PM
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
Nucleus
Apr 22nd, 2001, 11:16 PM
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?
kedaman
Apr 23rd, 2001, 04:13 AM
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.
Sastraxi
Apr 23rd, 2001, 02:45 PM
How 'bout this:
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!!
kedaman
Apr 23rd, 2001, 03:02 PM
thats definitely biased to give higher values for A and B
Jotaf98
Apr 24th, 2001, 11:00 AM
Try this:
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:
kedaman
Apr 24th, 2001, 11:14 AM
biased to give values close to 25
Jotaf98
Apr 24th, 2001, 03:05 PM
Ok, ok: this one should work :rolleyes:
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
kedaman
Apr 24th, 2001, 03:23 PM
Give up, and save yourself some time :p
uttamsaxena
Apr 29th, 2001, 01:47 PM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.