a little array help please

Posted By: badapple

a little array help please - 10/13/09 04:52

var array_choice1;
var array_choice2;
var array_choice3;
var array_choice4;
var array_choice5;

var my_array[10] = {0,10,20,30,40,50,60,70,80,90};


i need a small function that choses 5 variables from
my_array[10] and applies them to array_choice 1-5 but
doesnt chose the same array spot twice.
so if this function choses my_array[3] randomly first
then array_choice1 will be assinged that value and
my_array[3] cant be chosen again

and all must be chosen randomly

i really appreciate any help smile
Posted By: EvilSOB

Re: a little array help please - 10/13/09 05:52

Untested but I think its OK...
Code:
var array_choice1 = -1;
var array_choice2 = -1;
var array_choice3 = -1;
var array_choice4 = -1;
var array_choice5 = -1;

var my_array[10] = {0,10,20,30,40,50,60,70,80,90};

function populate-choices()
{  random_seed(0);   //really ranomize values
   //select first index
   array_choice1 = integer(random(10));
   //select second index
   while(array_choice2==-1)
   {  array_choice2 = integer(random(10));
      if(array_choice2==array_choice1)   array_choice2 = -1;
   }
   //select third index
   while(array_choice3==-1)
   {  array_choice3 = integer(random(10));
      if(array_choice3==array_choice1)   array_choice3 = -1;
      if(array_choice3==array_choice2)   array_choice3 = -1;
   }
   //select forth index
   while(array_choice4==-1)
   {  array_choice4 = integer(random(10));
      if(array_choice4==array_choice1)   array_choice4 = -1;
      if(array_choice4==array_choice2)   array_choice4 = -1;
      if(array_choice4==array_choice3)   array_choice4 = -1;
   }
   //select fifth index
   while(array_choice5==-1)
   {  array_choice5 = integer(random(10));
      if(array_choice5==array_choice1)   array_choice5 = -1;
      if(array_choice5==array_choice2)   array_choice5 = -1;
      if(array_choice5==array_choice3)   array_choice5 = -1;
      if(array_choice5==array_choice4)   array_choice5 = -1;
   }
   //now turn indexes into actual array values
   array_choice1 = my_array[array_choice1];
   array_choice2 = my_array[array_choice2];
   array_choice3 = my_array[array_choice3];
   array_choice4 = my_array[array_choice4];
   array_choice5 = my_array[array_choice5];
}

BEWARE of typo's....
Posted By: badapple

Re: a little array help please - 10/13/09 08:27

thank you evilsob i will try it.
Posted By: delinkx

Re: a little array help please - 10/13/09 09:40

here is another way [tested]

Code:
//array choices
var array_choice1;
var array_choice2;
var array_choice3;
var array_choice4;
var array_choice5;

var my_array[10] = {0,10,20,30,40,50,60,70,80,90};
var chosen_index_array[5];

//random variable
var myRandom;

//variables
var i,j;
var myflag;


void main()
{
	for (j=0;j<5;j++)
	{
		//seeds the random function
		random_seed(0);
		//flag
		myflag = 0;
		
		while(myflag == 0)
		{
			//choose a random index
			myRandom = integer(random(10));
			
			//check if index already chosen
			for(i=0;i<5;i++)
			{
			 if(myRandom == chosen_index_array[i])
			  {	
				myflag = 0;
				break;
			  }
			 myflag = 1;
			 
			}
			 
		}
	
		//adding index to array chosen
		chosen_index_array[j] = myRandom;
				
		if(j==0)
			array_choice1 = my_array[myRandom];
		if(j==1)
			array_choice2 = my_array[myRandom];
		if(j==2)
			array_choice3 = my_array[myRandom];
		if(j==3)
			array_choice4 = my_array[myRandom];
		if(j==4)
			array_choice5 = my_array[myRandom];
						
	
}
	
	
}


Posted By: MrGuest

Re: a little array help please - 10/13/09 11:38

here's some of my old code...

would use a little code to define and reset the array
Code:
//setup possible object number pairs
	int_objects = 10; //global int
	for(i = 0; i < int_objects; i+=2){
		arr_objects[i] = i*10; //*10 for (0, 10, 20 etc...)
	}



then use this to pick the number
Code:
function get_object_number(){
	
	random_seed(0);
	
	//find number
	int int_temp_chose, int_temp_pos;
	
	int_temp_pos = integer(random(int_objects));
	int_temp_chose = arr_objects[int_temp_pos];
	
	int i;
	for(i = int_temp_pos; i < int_objects; i++){
		arr_objects[i] = arr_objects[i+1];
	}
	
	int_objects--;
	return(int_temp_chose);
}


Posted By: badapple

Re: a little array help please - 10/14/09 08:09

thank you all for your help
© 2024 lite-C Forums