For loop, randomly. - iLearner - 03.02.2017
So, i was thinking if its possible to run for loop randomly... like instead of for( i =0; i< max; i++)
instead of i++, making it access random element, but while remaining within max
Re: For loop, randomly. -
Vince - 03.02.2017
Then how will the condition ever be fulfilled? It will just create an infinite loop.
Re: For loop, randomly. - iLearner - 03.02.2017
e.g:
i have this thingy: myarray[5];
so what we usually do is, for(int i=0; i< 5; i++) myarray[i] ... where i is respectively 0, 1, 2, 3, 4
What i want to do is somehow we can make that for loop run till it reaches 4, but the i value shouldn't be 0, 1, 2, 3, 4 but something like 4, 1, 3, 0, 2...
What i had in mind was making it something like this:
PHP код:
new max, myarray[max], mycount;
while(mycount < max)
{
new i = random(max);
myarray[i] ...
mycount++;
}
Any harm with this one?
Re: For loop, randomly. -
AndreiWow - 03.02.2017
But what if the random is instantly 4, like the loop start and the random number that will come is 4, which 4 is max, will the loop stop earlier without going through 2/1/0..?
Re: For loop, randomly. - iLearner - 03.02.2017
PHP код:
new max, myarray[max], mycount;
while(mycount < max)
{
new i = random(max);
myarray[i] ...
mycount++;
}
Lemme explain this piece of code... it means, the cycle will run till mycount has not reached max, and mycount is incrementing everytime it loops, meaning even if i is 4, the loop wont stop as the while loop is taking in consideration mycount, not i.
Re: For loop, randomly. -
Dayrion - 03.02.2017
This is my version xD
PHP код:
randomEx(min, max, ...) // credits Dayrion : ...
{
max++;
new
maxarg = numargs()-1,
nb = random(max - min) + min;
while(maxarg+1 != 2)
{
if(nb == getarg(maxarg))
{
maxarg = numargs()-1;
nb = random(max - min) + min;
}
else
maxarg--;
}
return nb;
}
main()
{
myarray[randomEx(1, 5)];
myarray[randomEx(1, 5, 5)];
myarray[randomEx(1, 5, 5, 4)];
myarray[randomEx(1, 5, 5, 4, 3)];
myarray[randomEx(1, 5, 5, 4, 3, 2)];
}
Re: For loop, randomly. -
PrO.GameR - 03.02.2017
Here's a solution
1- make a local array same size as your whole loop.
2- Randomly feel them with unique indexes 0 to max
3- make a normal loop, but use j=array[i] and use that
4- now you've made a proper random loop index.
Re: For loop, randomly. -
Jefff - 03.02.2017
https://sampforum.blast.hk/showthread.php?tid=38965&page=406