Question:
programming question (not sure how to classify the question)?
?
2011-06-21 16:30:28 UTC
So I am creating a program with a certain number of cars, with adjustable number of sedans, trucks and SUV's, all with different properties such as mass, etc.

I want to choose which cars are which randomly (there is a set up grid of cars)

It is a double array, I was wondering how could I go through the double array car[j][l] with my random number generator rand() and select each car as a sedan, truck and SUV randomly without missing any car or choosing any car twice?

for ex. i want something like
car[0][1] = truck
car[4][15] = sedan


I can think of maybe a way to do it recursively, but this is "inefficient".

Also, as a final note, I would settle for a nonexact amount as well, and instead have a percentage of cars being sedans, trucks etc, so it will not have to be perfect every time.

Thanks
Four answers:
green meklar
2011-06-21 21:53:04 UTC
Your question is somewhat confusing. It seems to me that what you want is to initialize the elements of the array such that the total numbers EACH of sedans, trucks and SUVs is some constant known in advance (algebraically, s+t+u = a*b where a and b are the dimensions of the array and s, t and u are the known-in-advance numbers of sedans, trucks and SUVs respectively), but their positions in the array are random.



Here's how I would do it. First, set up a 1D array (let's call it 'src', short for 'source') with a length of 3 (because you have 3 types of cars), and set each of its elements equal to the number of the associated type of car. For instance, let's say sedans have index 0, trucks have index 1 and SUVs have index 2, and let's say you have a 3x3 array you want to fill randomly. That means the elements in src must all be nonnegative and add to 9. For instance, we could do the following:

src[0]=5;

src[1]=2;

src[2]=2;

which would mean 'five sedans, two trucks and two SUVs'. Now, iterate through the 2D array in a simple, nonrandom fashion. But keep a variable around representing the number of cars remaining to be assigned, let's call it 'rem', short for 'remaining'. For instance, in the above example, we would initialize it to 9, and subtract 1 on completion of every inner loop iteration in the 2D array. For each cell, get a random integer from 1 to rem, then iterate upwards through src subtracting the corresponding value in src from rem until it goes to 0 or less, in which case stop, assign that type of car to that cell, and subtract 1 from the corresponding element in src and from rem.



The following Java code (where rand(int min,int max) is assumed to return a random integer from min to max inclusive; car is the double array passed in to be initialized; src is the externally initialized array fitting the properties described above) implements this logic:



public static void populate(double[][] car,int[] src)

{

int rem=0;

int i=0;

for(;i
{

rem+=src[i];

}

for(int x=0;x
{

for(int y=0;y
{

int p=rand(1,rem);

for(i=0;i
{

p-=src[i];

if(p<=0)

{

break;

}

}

car[x][y]=(double)i;

rem--;

src[i]--;

}

}

}



Implementations for C and C++ would be a little harder, because they don't store array length explicitly, so you would have to pass in a few extra arguments. I haven't actually tested the above code, but at least you can see the principle of what's going on here. Notice how instead of randomly selecting the cell and putting in a deterministic car type, we're randomly selecting the car type and putting it in a deterministic cell. This is faster and achieves the same logical result. You may of course have to adjust the code a bit to conform to whatever model you're fitting this into.
?
2011-06-22 01:52:01 UTC
There's a way to use a single loop and convert the loop variable into row, col without duplication.



Let's say that your array is car[row][col];



Then this is how to put a randome int between 0 and 3 inclusive in each space:

int r,c;

for( int k=0; k
...r = k/col; // try some values, you'll see how it works!

...c = k%row;

,,,car[r][c] = rand.nextInt(4);

}





You should use the Random class:

import java.util.Random;



defind a randolm object:

Random rand = new Random();



Let's see some code if you need more help.



+add

You should probably define your own Car class with fields

String type; // SUV, Sedan, etc

double mass;

// and whatever else



Then define a method that generates a random car (change to Vehicle, since a Truck is not a car).



This is the whole point of object oriented progarmming: your objects are vehicles with different properties. You then store arrays of Car objects or, even better, array lists of Cars.
Tiberia
2011-06-21 23:51:53 UTC
If I understand correctly, you want to hit every car in the array, but in random order. Let's assume it's a 10 by 10 array. What I'd do is have rand generate numbers 0-9, store the output in an 1D array - but check that the number has not already been stored. (This is the same as creating a Set.) Use this array to determine the order that you're gonna hit the rows. Then, for each row, do the same thing to determine the order to hit the columns. It's not very efficient, but it will ensure that you hit every spot in a random order each time.



What does your recursive method do?

===

Tasm's method does *not* ensure that you haven't missed anything or hit anything twice.

===

If you want to create such an array:

Java Code - Assumes your rand() works and is set up to only return 1, 2, or 3 (or any 3 digits), and assumes the array holds a superclass of type Vehicle

(Code syntax might be slightly off... sorry.)



Vehicle[][] car = new Vehicle[10][10]; //create array of type Vehicle called "car"

for(int i=0; i
for(int j=0; j
int r = rand(); //generate random number 1,2,3

switch(r){ //interpret numbers into vehicles

case 1: car[i][j] = new Car(); break;

case 2: car[i][j] = new Suv(); break;

case 3: car[i][j] = new Truck();

default: break;

}

}

}
Tasm
2011-06-22 00:15:45 UTC
loop until the car array is empty.



Then do a random variable to the length of the array car[] = X

then another random variable for length of array car[X][] = Y



Then remove car[X][Y] from the array


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...