SA-MP Forums Archive
Choosing random variable - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Choosing random variable (/showthread.php?tid=603543)



Choosing random variable - Aa12 - 24.03.2016

Код:
new a[5];

a[0]=1;
a[1]=4;
a[2]=0;
a[3]=7;
a[4]=0;
I need to find which of these variables aren't equal zero and then randomly choose on of them (of those that aren't equal 0).

I guess start should look something like this...
Код:
for(new i; i<5; i++)
{
 if(a[i]!=0)
  {
  // dont know what to do
  }
}



Re: Choosing random variable - SickAttack - 24.03.2016

pawn Код:
new a[5];

a[0]=1;
a[1]=4;
a[2]=0;
a[3]=7;
a[4]=0;

----------------------------------

new b[5], count;
for(new i = 0; i < 5; i ++)
{
    if(a[i] != 0)
    {
        b[count] = a[i];
        count ++;
    }
}

b[random(count)]; // select a random number



Re: Choosing random variable - Aa12 - 24.03.2016

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
pawn Код:
new a[5];

a[0]=1;
a[1]=4;
a[2]=0;
a[3]=7;
a[4]=0;

----------------------------------

new b[5], count;
for(new i = 0; i < 5; i ++)
{
    if(a[i] != 0)
    {
        b[count] = a[i];
        count ++;
    }
}

b[random(count)]; // select a random number
I forgot to mention, variables arrays can't change. For example, according to your code if a[0]=0, a[1]=0, a[2]=1 so b[1]=a[2] when I want it to be b[2]=a[2]

and it would be better if other variables woudnt be used to save a[] values


Re: Choosing random variable - SickAttack - 24.03.2016

In that case:
pawn Код:
new a[5];

a[0]=1;
a[1]=4;
a[2]=0;
a[3]=7;
a[4]=0;

----------------------------------

new b[5][2], count;
for(new i = 0; i < 5; i ++)
{
    if(a[i])
    {
        b[count][0] = a[i];
        b[count][1] = i;
        count ++;
    }
}

b[random(count)]; // select a random number
b[count][0] = the value
b[count][1] = the index

Also "if(a[i] != 0)" is the same as "if(a[i])".


Re: Choosing random variable - Aa12 - 24.03.2016

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
In that case:
pawn Код:
new a[5];

a[0]=1;
a[1]=4;
a[2]=0;
a[3]=7;
a[4]=0;

----------------------------------

new b[5][2], count;
for(new i = 0; i < 5; i ++)
{
    if(a[i])
    {
        b[count][0] = a[i];
        b[count][1] = i;
        count ++;
    }
}

b[random(count)]; // select a random number
b[count][0] = the value
b[count][1] = the index

Also "if(a[i] != 0)" is the same as "if(a[i])".
Did not worked, for some reason when I try to print b value it always prints "20"
Btw, I realised this thread was not informative enough for my problem so I created a new one : http://forum.sa-mp.com/showthread.ph...69#post3675969