Random & Array Help [Slightly Advanced]
#1

Hello.

In order to familiarize my self with the random function, I decided to add three static vehicles. Now I chose three vehicle ID's, and I have been trying to add 3 cars to my script but I want the vehicle ID to be picked randomly. I want it either to spawn Vehicle ID 404, 401, or 405.

So I looked up the random function on the wiki, and I am completely lost on arrays. I have spent the last 2 hours or so reading tutorials, but it is not coming to me at all. If anyone experienced with arrays would be kind enough to provide a detailed explanation (just a piece of code with some comments will do) on 2D arrays, and on how to randomize them into AddStaticVehicle. I would greatly appreciate it.

This is my array for the vehicle ID's I wish to be randomly picked from.
pawn Код:
new RandomVehicles[][3] =
{
    {404},
    {401},
    {405}
};
This is the AddStaticVehicle part that I fail catastrophically on.
pawn Код:
AddStaticVehicleEx(RandomVehicles[rand][3],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar1
AddStaticVehicleEx(RandomVehicles[rand][3],-902.2551,2707.3286,42.4701,223.0923,-1,0.60000); // RandCar2
AddStaticVehicleEx(RandomVehicles[rand][3],-911.8376,2697.6580,42.4780,225.7278,-1,0,60000); // RandCar3
Reply
#2

pawn Код:
new randcheck;
random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[randcheck][0],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000);
random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[randcheck][0],-902.2551,2707.3286,42.4701,223.0923,-1,0.60000);
random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[randcheck][0],-911.8376,2697.6580,42.4780,225.7278,-1,0,60000);
Try it this way.
Reply
#3

you dont need a 2d array for this,
the reason the wiki using one is each row is storing 3 values...

so code like this should work,

pawn Код:
new RandomVehicles[] =
{
    404,
    401,
    405
};


new rand = random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[rand],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar1
random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[rand],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar2
random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[rand],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar3
it wont seam very random with just 3 values however
there is a 33.333333333% chance that each car will be called


hope this helps,
Reply
#4

Quote:
Originally Posted by Jonny5
Посмотреть сообщение
you dont need a 2d array for this,
the reason the wiki using one is each row is storing 3 values...

so code like this should work,

pawn Код:
new RandomVehicles[] =
{
    404,
    401,
    405
};


new rand = random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[rand],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar1
random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[rand],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar2
random(sizeof(RandomVehicles));
AddStaticVehicleEx(RandomVehicles[rand],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar3
it wont seam very random with just 3 values however
there is a 33.333333333% chance that each car will be called


hope this helps,
Thank you for your reply, but it's only spawning a Perrenial (404). To test if it was really outputting a random vehicle ID, I made a /respawncars command to see if it would change. But it still keeps spawning a Perrenial.
Reply
#5

yeah thats the trouble with any random function
this has been a problem for me for years in MANY languages.
Im actually a newbie to pawn but the fundamentals still apply

im sure what your doing is limiting you to just 3

but just as a test to know it is in fact random
maybe make the array with 12 ids (just for testing)
this would give a 8.3% chance of each car being spawned.
they can even be the same ids just over and over.

i think the last time i had this problem was with a webpage (vbscript)
ill see if i can dig that up and see what kind of logical solution i came up with back then.

one thing that may or may not help (this would depend on pawns underlying logic)
is the code i posted uses the same rand var for all three spawns

maybe remove that var all together and use it inline like

pawn Код:
new RandomVehicles[] =
{
    404,
    401,
    405
};



AddStaticVehicleEx(RandomVehicles[random(sizeof(RandomVehicles))],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar1
AddStaticVehicleEx(RandomVehicles[random(sizeof(RandomVehicles))],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar2
AddStaticVehicleEx(RandomVehicles[random(sizeof(RandomVehicles))],-901.9831,2687.4890,42.4714,43.9074,-1,0,60000); // RandCar3
i know some language use a tick count to calculate the random number(im not sure of all the logic but)
im not sure how pawn does there random.. sorry, but if so this would explain the same spawn as the code
its excuted so quickly..
let me know how this turned out for you
im interested to know the results.

regards,


edit:

also if you respawned the cars it will keep the model id always.
once you add the vehicle you cannot change the model id that im aware of,
respawning the car would do just that,
so scratch that test all together

a better test would be to use a loop and print the random number maybe 50 times...
this will tell all


pawn Код:
new RandomVehicles[] =
{
    404,
    401,
    405
};
new rdTest;
for(rdTest=0;rdTest<50;rdTest++){
    printf("%i",RandomVehicles[random(sizeof(RandomVehicles))]);
}
note i did this and my numbers come out looking like...
401
401
401
405
405
404
405
401
405
405
404
405
401 ...... and on and on


so as you see in the first 3 tried its found the same number..


hope this helps
Reply
#6

Sweet, I will add more vehicles to the array and see if the chances of random vehicles increase.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)