
Randomizer
I have 10 values that need to add up to 100%. How would one create a randomizer that changes the values but keep the total to %100?<BR><BR>Thanks,

RE: Randomizer
I don't think it can be done (Now watch people prove me wrong)

RE: Randomizer
Yeah, that's a tough one. <BR><BR>Thought<BR>You could kind of cheat and set up a bunch of arrays of 10 values each that each add to 100 & then select the array randomly...<BR>

Well, this is rather simple actually...
... All you need to do is create 9 random values between 0% and 10% and then calculate the 10th one as 100%  SUM(previous 9 values).<BR><BR>Something like (this is pseudocode!!):<BR><BR>Dim Random(10), idx, sum<BR><BR>sum = 0<BR>For idx = 1 To 9<BR>&nsbp; Random(idx) = Rnd() * 10 ' This is to create a random value between 0 and 10<BR>&nsbp; sum = sum + Random(idx)<BR>Next idx<BR><BR>Random(10) = 100  sum<BR><BR>All 10 values should add up to 100.<BR><BR>:)<BR><BR>Oliver.

Sorry, the &nsbp was supposed...
...to be a nonbreakable space. :/<BR><BR>Oliver.

RE: Well, this is rather simple actually...
O  <BR>That's pretty cool. <BR><BR>I have a question  wouldn't this only provide one number greater than 10 ? There could be 9 values of 1 determined by the random(idx) and then one of 91, right? <BR><BR>Wonder if there is a way to NOT limit the highest number to only the last value... anybody ?<BR>

Best I can come up with..
.. (for only spending 15 minutes or so):<BR>[code language="VBScript"]<BR>Dim aNumbers(9)<BR>Dim iCnt, _<BR> iRandomNumber, _<BR> iTotalRandom, _<BR> iMaxRandom<BR>Dim sMsg<BR><BR>Randomize Timer<BR><BR>iTotalRandom = 0<BR><BR>For iCnt = 0 To UBound(aNumbers)  1<BR> iMaxRandom = 100  (UBound(aNumbers)  iCnt)  iTotalRandom<BR><BR> iRandomNumber = Int((iMaxRandom  1 + 1) * Rnd + 1)<BR><BR> MsgBox "iCnt = " & iCnt & vbCrLf & _<BR> "iMaxRandom = " & iMaxRandom & vbCrLf & _<BR> "iRandomNumber = " & iRandomNumber & vbCrLf & _<BR> "iTotalRandom = " & iTotalRandom & vbCrLf<BR><BR> aNumbers(iCnt) = iRandomNumber<BR> iTotalRandom = iTotalRandom + iRandomNumber<BR>Next<BR><BR>aNumbers(UBound(aNumb ers)) = 100  iTotalRandom<BR><BR>sMsg = "Processing Total = " & iTotalRandom & vbCrLf<BR>iTotalRandom = 0<BR>For iCnt = 0 To UBound(aNumbers)<BR> iTotalRandom = iTotalRandom + aNumbers(iCnt)<BR><BR> sMsg = sMsg & iCnt & " = " & aNumbers(iCnt) & " (" & iTotalRandom & ")" & vbCrLf<BR>Next<BR>sMsg = sMsg & "Final Total = " & iTotalRandom & vbCrLf<BR><BR>MsgBox sMsg<BR>[/code]

RE: Best I can come up with..
Thanks Doug<BR><BR>I had to add some constants for this to work. Thanks for your help.<BR><BR>Const xlArea = 1<BR> Const xlBar = 2<BR> Const xlColumn = 3<BR> Const xlLine = 4<BR> Const xlPie = 5<BR> Const xlRadar = 4151<BR> Const xlXYScatter = 4169<BR> Const xlCombination = 4111<BR> Const xl3DArea = 4098<BR> Const xl3DBar = 4099<BR> Const xl3DColumn = 4100<BR> Const xl3DLine = 4101<BR> Const xl3DPie = 4102<BR> Const xl3DSurface = 4103<BR> Const xlDoughnut = 4120<BR> <BR> Const xlRows = 1<BR> Const xlColumns = 2

That's very easy too. Try this...
...(again it's pseudocode):<BR><BR>Dim Random(10), idx, sum<BR><BR>For idx = 1 To 10<BR> Random(idx) = 0 ' Set all values to zero<BR>Next idx<BR><BR>sum = 0<BR>For idx = 1 To 10<BR> Random(idx) = Rnd() * 100 ' This is to create a random value between 0 and 100 <BR> sum = sum + Random(idx)<BR><BR> If sum > 100 Then<BR> Exit For Loop<BR> End If<BR>Next idx<BR><BR>However, the problem here is that some values may now be zero. You could adapt the random value creation, so that it only creates values between 0 and 50, for example. Up to you.<BR><BR>Hope this helps,<BR>Oliver.
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

