Randomized array members.
Hello everyone,
I'm rather new to Java and not overly experienced with programming in general. I am designing algorithms myself in order to improve, and I'm kind of stuck in a problem I created.
I have an array of objects with a certain size, say 1000. These objects' constructor takes a value as an argument. I have a variety of values that I can assign, but a certain value can only be assigned so many times in the array. For example I want to fill my array with 800 objects initialized with value a, 20 with value b, 1 with value c etc etc.
The position of objects in the array is relevant and it is also necessary that they are placed randomly. However, each object I create I want to have a chance of taking a specific value relevant to the number of times that value can yet be applied. Specifically because I want most of my objects to take a single value, I do not want all other values to be crammed either in the beginning (as would happen if I used equal chances until I ran out of the other values) or the end (as would happen if I worked percentage wise). I need everything to be dispersed across the array relatively evenly.
So, how do I go about it? Any ideas?
Thanks in advance.
Re: Randomized array members.
Have you actually tried any code for this?
Can you please post that if available?
Goldest
Re: Randomized array members.
I don't have my project with me, but I'll try and demonstrate my process. I've gone about it in the regular percentage method I described.
Code java:
private int array_size;
private int val1, val2, val3, val4, val5;
private my_class[] arr;
private float val1per, val2per, val3per, val4per, val5per;
public class build_array(){
get_values();
arr = new my_class[array_size];
for(int i=0;i<array_size;i++){
val1_per = val1/array_size;
val2_per = val2/array_size;
val3_per = val3/array_size;
val4_per = val4/array_size;
val5_per = val5/array_size;
if(my_random() = val1){
arr[i] = new my_class(val1);
val1--;}
// do for all values
}
}
public int my_random(){
// evenly split 100 between the five value percentages rounded, can't remember what I did here exactly
// insert some way to get a random number from 1 to 100, again don't remember what I used
// match that number with a variable and return it
}
public void get_values(){
// got some file reading done here, but I'll simplify it
array_size = 100
val1 = 70;
val2 = 18;
val3 = 1;
val4 = 4;
val5 = 7
}
But like I said, this hasn't been working very well for me, there's an extremely small chance of getting any other value than val1 (in this example it's still plausible, but with bigger numbers and larger differences it wouldn't happen) in the first members of the array. In other words, though my algorithm does what I intend to more or less, the placement of the values across the array members is uneven.
Re: Randomized array members.
If all your values in the array are single characters, make a char array.
Re: Randomized array members.
public class build_array(){
this will make an error.
You're both trying to define the beginning of a class and making that beginning into a constructor at the same time.
Lose the () after array.
Re: Randomized array members.
arr = new my_class[array_size];
You never defined array_size. You declared it but never gave it a value.
That also will make an error.
If you're allowed to, why not pass the array size to the constructor?
Re: Randomized array members.
Quote:
Originally Posted by
javapenguin
arr = new my_class[array_size];
You never defined array_size. You declared it but never gave it a value.
I did, in that function I called.
Quote:
Originally Posted by
javapenguin
public class build_array(){
this will make an error.
You're both trying to define the beginning of a class and making that beginning into a constructor at the same time.
Lose the () after array.
Woops, slipped. As I said I typed this out from memory as I don't have the project with me.
Quote:
Originally Posted by
javapenguin
If all your values in the array are single characters, make a char array.
They're objects of a different class I created, I just didn't type it here.
I should clarify that my code functions as I explained, just doesn't do what I want it to. I'm looking for a way to evenly distribute different values (that appear with different frequencies) along an array without stacking most of them in either the beginning or the end of the array.
Re: Randomized array members.
Code java:
public class build_array
{
private int array_size;
// private int val1, val2, val3, val4, val5;
private int[] vals;
private my_class[] arr;
// private float val1per, val2per, val3per, val4per, val5per;
private float[] valpers;
private Scanner inFile;
public build_array()
{
inFile = new Scanner("FileName.extension");
// I'm assuming array_size is the first value scanned in
vals= new int[6];
valper = new float[5];
int v = 0;
while (v < 5)
{
vals[v] = Integer.parseInt(inFile.nextLine());
v++;
}
// get_values();
arr = new my_class[vals[0]];
for(int i=0;i<vals[0];i++){
valper[i] = vals[i+1]/vals[0];
if(my_random() == vals[1]){
arr[i] = new my_class(vals[1]);
int whatever = vals[1];
whatever = whatever - 1;
vals[1] = whatever;
}
// do for all values
}
}
public int my_random(){
// evenly split 100 between the five value percentages rounded, can't remember what I did here exactly
// insert some way to get a random number from 1 to 100, again don't remember what I used
// match that number with a variable and return it
}
// the method below does nothing that I'm aware of.
public void get_values(){
// got some file reading done here, but I'll simplify it
array_size = 100
val1 = 70;
val2 = 18;
val3 = 1;
val4 = 4;
val5 = 7
}