XuN 
Sphere Developer
    
Posts: 852
Likes Given: 102
Likes Received: 156 in 119 posts
Joined: Jul 2013
Reputation: 30
![]()
|
RE: How to compare random values
<R20> returns a random number between 1 and 20 (a total of 20 possibilities), <r0,20> returns a random number between 0 and 20 (21 numbers in total).
BTW, now that you realised to do it, and learned in the proccess i'll show you a function I had for this:
Code:
[function GetRandomsFromTotal] //GetRandomsFromTotal 5,100 returns 5 random* numbers wich sum 100. The random is calculated this way: BaseNum=(100/5), Result=RAND(BaseNum - argv[0],Basenum + argv[0])
local.amount=<argv[0]>
local.total=<argv[1]>
local.max=<qval (<isempty <argv[2]>>==0)?<argv[2]>:<eval (<argv[1]>/<argv[0]>)+<argv[0]>>>
local.min=<qval (<isempty <argv[3]>>==0)?<argv[3]>:<eval (<argv[1]>/<argv[0]>)-<argv[0]>>>
if (<local.min><0)
local.min=0
endif
local.res=0
for 1 <local.amount> // Here the results are created, nothing is checked so all of then can be the max <local.max> or the min... shit happens?
local.ret_<dlocal._for>=<r<local.min>,<local.max>>
local.res +=<local.ret_<dlocal._for>>
endfor
local.pointer=1
while <local.res>><local.total> // So this is a check preventing the total result being higher than <argv[1]> and any result to be lower than <local.min>
if (<local.ret_<dlocal.pointer>>><local.min>)
local.ret_<dlocal.pointer> -=1
local.res --
endif
local.pointer ++ // local.pointer is just going throught all the results decreasing (in this case) or increasing (in the next while) all the values by 1 until the requirements are done.
if (<local.pointer>><local.amount>)
local.pointer=1
endif
endwhile
while <local.res><<local.total> // and this one prevents anyone being lower than <argv[1]>
if (<local.ret_<dlocal.pointer>><<local.max>)
local.ret_<dlocal.pointer> +=1
local.res ++
endif
local.pointer ++
if (<local.pointer>><local.amount>)
local.pointer=1
endif
endwhile
//so with these 2 whiles Sphere ensured that all the values does sum the total of <argv[1]> we create the return value.
local.ret=0 //first value will be a 0 so we don't check in every for loop if the local is empty to create it or not.
for 1 <local.amount>
local.ret=<local.ret>,<dlocal.ret_<dlocal._for>>
endfor
return <streat <local.ret>> // so here we remove the first value (the 0) and return the rest.
This will return the values separated with commas, ie: local.res=<GetRandomsFromTotal 5,20> will return a total of 5 random values which, in total, do not pass from 20. It also applies some logic to the calcs... numbers have a minimum value and a maximum value: in this case min value would be (20/5)-5=-1 and it will be fixed to 0 if that happens, the max would be (20/5)+5=9. This is done to create values between some certain range that let Sphere do its calcs easier. It won't be nice if the first random is 20 and the rest have to be 0 IMO.
(This post was last modified: 06-18-2014 07:28 PM by XuN.)
|
|
06-18-2014 07:19 PM |
|
The following 1 user Likes XuN's post:1 user Likes XuN's post
kn4tseb (06-19-2014)
|