Randomizer

1. Senior Member
Join Date
Dec 1969
Posts
486

## 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,

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: Randomizer

I don&#039;t think it can be done (Now watch people prove me wrong)

3. Member
Join Date
Dec 1969
Posts
66

## RE: Randomizer

Yeah, that&#039;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>

4. Senior Member
Join Date
Dec 1969
Posts
5,955

## 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 pseudo-code!!):<BR><BR>Dim Random(10), idx, sum<BR><BR>sum = 0<BR>For idx = 1 To 9<BR>&nsbp;&nbsp;Random(idx) = Rnd() * 10 &#039; This is to create a random value between 0 and 10<BR>&nsbp;&nbsp;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.

5. Senior Member
Join Date
Dec 1969
Posts
5,955

## Sorry, the &nsbp was supposed...

...to be a non-breakable space. :-/<BR><BR>Oliver.

6. Member
Join Date
Dec 1969
Posts
66

## RE: Well, this is rather simple actually...

O - <BR>That&#039;s pretty cool. <BR><BR>I have a question - wouldn&#039;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>

7. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## 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]

8. Senior Member
Join Date
Dec 1969
Posts
486

## 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

9. Senior Member
Join Date
Dec 1969
Posts
5,955

## That's very easy too. Try this...

...(again it&#039;s pseudo-code):<BR><BR>Dim Random(10), idx, sum<BR><BR>For idx = 1 To 10<BR>&nbsp;&nbsp;Random(idx) = 0 &#039; Set all values to zero<BR>Next idx<BR><BR>sum = 0<BR>For idx = 1 To 10<BR>&nbsp;&nbsp;Random(idx) = Rnd() * 100 &#039; This is to create a random value between 0 and 100 <BR>&nbsp;&nbsp;sum = sum + Random(idx)<BR><BR>&nbsp;&nbsp;If sum &#062; 100 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;Exit For Loop<BR>&nbsp;&nbsp;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
•