add the integers from 0 to 9 to a List
pick one at random from the list
delete it
repeat
(note that the list size will decrease every time you delete something from it so write your random() call so that it uses the current list size as the maximum)
What have you tried? This is a pretty basic problem and it's processing, so do you need the perfect solution or could you perhaps come up with something that works? Besides, this is the kind of problem that already has a lot of answers on stackoverflow.
Some possible solutions:
Put all the numbers in a list, then shuffle and move through it in sequence.
Put all the numbers in a list, choose one randomly, then remove it from the list.
Pick a random number, use it and add it to the list, pick another random number, compare it to the list, if present retry, if not present use it and add it to the list.
Have your objects in two lists, a toBePicked list and a alreadyPicked list. Randomly pick objects from the first list and move them to the second. In the end, the second list has all the objects in a random order.
As usual "best way" is not that useful without some criteria to judge solutions.
int s = 20, t=100; // t is the range of the random numbers form 0-t
// s = numbers of random numbers (How many random number you want to generate)
int[] data = new int[s];
String saveMe[] = new String[s];
ArrayList<Integer> datax = new ArrayList();
void setup() {
size(300, 100);
}
void draw() {
background(-1);
// generate randomo numbers
for (int i=0;i<data.length;i++) {
int x =(int)random(0, t+1);
datax.add(x);
for (int j=0;j<datax.size();j++) {
int t = (int) datax.get(j);
if (t!=data[i]) {
data[i] = x;
//--just for looking coool! :)
fill(0);
text(x, i*20, height/2);
}
}
}
//--Sort the data if you want or commment it
data = sort(data);
//---- save the data in string array for wrting in a text file
int l=0;
for (int k = 0; k < data.length; k++)
{
saveMe[k] = str(data[k]);
l = l+ data[k];
}
//----Take the average of all 20 values and exits from the program
if (l/20==50) {
println(l/20);
saveStrings("data.txt", saveMe);
exit();
}
}
Answers
(note that the list size will decrease every time you delete something from it so write your random() call so that it uses the current list size as the maximum)
What have you tried? This is a pretty basic problem and it's processing, so do you need the perfect solution or could you perhaps come up with something that works? Besides, this is the kind of problem that already has a lot of answers on stackoverflow.
Some possible solutions:
As usual "best way" is not that useful without some criteria to judge solutions.
A simple example just to get you started: %%-
Here is my take :)
A side question what is the goal of using
final
inside draw() loop?For local variables like that idx, it's merely an annotation that it promises to keep its once-set value until the end of its scope!
I see, thanks.
Thank you so much koogs, amnon, GoToLoop, and blyk. This forum is awesome because of members like you :)>-