# selecting random weighted variables

## Recommended Posts

My script needs to pick a random number between 2 and 13, but the chance of each number coming up needs to be altered according to user choices (GUI).

There must be a better way to do this with arrays or something..

thanks,

Func getInterval()

\$2=_GUICtrlSlider_GetPos(\$slider2)

\$3=_GUICtrlSlider_GetPos(\$slider3)+\$2

\$4=_GUICtrlSlider_GetPos(\$slider4)+\$3

\$5=_GUICtrlSlider_GetPos(\$slider5)+\$4

\$6=_GUICtrlSlider_GetPos(\$slider6)+\$5

\$7=_GUICtrlSlider_GetPos(\$slider7)+\$6

\$8=_GUICtrlSlider_GetPos(\$slider8)+\$7

\$9=_GUICtrlSlider_GetPos(\$slider9)+\$8

\$10=_GUICtrlSlider_GetPos(\$slider10)+\$9

\$11=_GUICtrlSlider_GetPos(\$slider11)+\$10

\$12=_GUICtrlSlider_GetPos(\$slider12)+\$11

\$13=_GUICtrlSlider_GetPos(\$slider13)+\$12 ;total of all the weighted variables

\$Int=random(1,\$13,1)

Select

case \$Int<\$3

return 2

case \$Int<\$4

return 3

case \$Int<\$5

return 4

case \$Int<\$6

return 5

case \$Int<\$7

return 6

case \$Int<\$8

return 7

case \$Int<\$9

return 8

case \$Int<\$10

return 9

case \$Int<\$11

return 10

case \$Int<\$12

return 11

case \$Int<\$13

return 12

case \$Int>\$12

return 13

EndSelect

EndFunc

##### Share on other sites

for the code below to work you need to put your sliders into an array + declare a array numberarray which holds all the values for \$2,\$3,\$4 etc.

it might not work at once, but i hope this helps you in the right direction.

```Func getInterval()

for \$i=2 to 13 step 1
if \$i>2 Then
\$numberarray[\$i]=_GUICtrlSlider_GetPos(\$slider[\$i])+\$numberarray[\$i-1]
Else
\$numberarray[\$i]=_GUICtrlSlider_GetPos(\$slider[\$i])
Endif
Next

\$Int=random(1,\$numberarray[13],1)

for \$j=3 to 13 step 1
if \$Int<\$numberarray[\$i] Then
return \$j-1
Endif
Next

if \$int>\$numberarray[12] Then
return 13
Endif

Endfunc```
Edited by cageman

##### Share on other sites

Thanks, that's quite useful,

cheers

## Create an account

Register a new account