Quote:
Originally Posted by SpeeDeeR
So lets say i have twenty items and i have to draw one and there is random chance for that.So far so good.But the thing is, that every item must be chosen once.And to clear things up this way there has to be twenty different draws with one different item.
|
This will give you a random number ranging from 1-20, with each only being returned once. After the twentieth item is used\selected, the selected-numbers will be cleared and you will be able to obtain a new set of 1-20, again each being returned only once. I'm not sure if you were using 1-20 just for example purposes or if it is what you actually needed. Let me know and I can fix it.
PHP Code:
RandomNumber()
{
static iSelected , iRandom;
if ( iSelected == 0x1FFFFE )
iSelected = 0;
for ( ;; )
{
iRandom = random_num( 1 , 20 );
if ( !( iSelected & ( 1 << iRandom ) ) )
{
iSelected |= ( 1 << iRandom );
return iRandom;
}
}
return 0;
}
__________________