For loop, randomly.
#1

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
Reply
#2

Then how will the condition ever be fulfilled? It will just create an infinite loop.
Reply
#3

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 maxmyarray[max], mycount;
    while(
mycount max)
    {
        new 
random(max);
        
myarray[i] ...
        
mycount++;
    } 
Any harm with this one?
Reply
#4

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..?
Reply
#5

PHP код:
 new maxmyarray[max], mycount
    while(
mycount max
    { 
        new 
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.
Reply
#6

This is my version xD
PHP код:
randomEx(minmax, ...) // credits Dayrion : ...
{
    
max++;
    new
        
maxarg numargs()-1,
        
nb random(max min) + min;
    while(
maxarg+!= 2)
    {
        if(
nb == getarg(maxarg))
        {
            
maxarg numargs()-1;
            
nb random(max min) + min;
        }
        else
            
maxarg--;
    }
    
    return 
nb;
}
main()
{
    
myarray[randomEx(15)];
    
myarray[randomEx(155)];
    
myarray[randomEx(1554)];
    
myarray[randomEx(15543)];
    
myarray[randomEx(155432)];

Reply
#7

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.
Reply
#8

https://sampforum.blast.hk/showthread.php?tid=38965&page=406
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)