Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → equitable random
equitable random
2002-11-10, 12:35 AM #1
How to do an equitable random ?


rand gives [0-1], or [0-1[ or ]0-1] or [0-1[ ?

i think its [0-1]

( 6] = 6 include, 6[ = 6 exclude )


Now if I want a rand that returns a int
between 0 and x-1

ex : x=5
i want 0-1-2-3- or 4

i= will be the array indice.

what the correct formule ?

i=x*rand ?

if i do this, i my experience was good, i could get x, so you can have an ArrayOutOfBound

i=(x-1)*rand ?

same, you only get i=x-1 if rand gives 1 => 1/100 chance ?


because i=int/rand=flex and i dont know how to trunc/assign correctly...

[This message has been edited by TCNF_Merfolk (edited November 10, 2002).]
2002-11-10, 1:12 AM #2
I also think it's a [0 - 1] interval. You can have a simple if(i == 5) check after the computation to make sure it does not get out of bounds...
You can use the flex values as array indices, because cog does automatically convert them using simple truncation. If it does not work nevertheless, do a manual conversion:
Code:
symbols
int i local
flex j local
end

code
whatever:
j = rand()*whatever;
i = j;
//and so on;
Return;
end



------------------
"Häb Pfrässe, süsch chlepfts!" - The coolest language in the world (besides Cherokee)

[This message has been edited by zagibu (edited November 10, 2002).]
"Häb Pfrässe, süsch chlepfts!" - The coolest language in the world (besides Cherokee)

↑ Up to the top!