PDA

View Full Version : Random Arrays


Luis-
06/03/2015, 02:05 PM
Hey! I've just thought of an idea but don't know if it'll work, to save me time coding it i'd rather ask here first.

Would it be possible to have a few different arrays with coords in them, all named "Array" with a number at the end, "1, 2, 3 etc" and then have a function to choose one of the arrays at random? Maybe formatting a string, "format(string, sizeof string, "Array%d", random(5));"

Surely it must be possible.

Kaliber
06/03/2015, 02:08 PM
Just make it easy and make a multidimensional array like this:

stock const array[][] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};

printf("%d",array[random(sizeof array)][random(sizeof(array[]))]);

Greekz :cool:

Luis-
06/03/2015, 02:13 PM
What if i had multiple coords in one array though?

RedFusion
06/03/2015, 02:17 PM
one 3d array should suffice.

new Float:testarray[2][2][3] =
{
{
{0.0, 0.0, 0.0},
{1.0, 1.0, 1.0}
},{
{0.0, 0.0, 0.0},
{1.0, 1.0, 1.0}
}
};

getRandomArray(&Float:x, &Float:y, &Float:z)
{
new
first_index = random(sizeof testarray),
second_index = random(sizeof testarray[]),
x = testarray[first_index][second_index][0],
y = testarray[first_index][second_index][1],
z = testarray[first_index][second_index][2]
;
}

Kaliber
06/03/2015, 02:18 PM
What if i had multiple coords in one array though?
Can you make an Example, like this:

stock const Float:coords[][3] = {
{x,y,z},
{x1,y1,z1},
{x2,y2,z2}
};

new r = 1;
printf("X: %f | Y: %f | Z: %f",coords[r][0],coords[r][1],coords[r][2]);
//-> Would be: X: x1 | Y: y1 | Z: z1

new r = random(sizeof coords); //That would coose random coords

Greekz :cool:

Luis-
06/03/2015, 02:21 PM
Well, i'm wanting to do it for my DMV system, instead of having just one route to use, I was thinking of having a couple which would be chosen at random. So, if the script chose array1 it'd show the checkpoints for array1, if it chose array2 it'd show array2 checkpoints.

Kaliber
06/03/2015, 02:25 PM
Well, i'm wanting to do it for my DMV system, instead of having just one route to use, I was thinking of having a couple which would be chosen at random. So, if the script chose array1 it'd show the checkpoints for array1, if it chose array2 it'd show array2 checkpoints.
Ahh i understand ok.

Well, then make sth like this:

switch(random(MAX_ROUTS)) {
case 0: //here you start route 1
case 1: //here you start route 2
case 2: //here you start route 3
//...
}

Make it like this..its the easiest way xD

Luis-
06/03/2015, 02:43 PM
Got it working, thank you :)