We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › list of random numbers
Page Index Toggle Pages: 1
list of random numbers (Read 583 times)
list of random numbers
May 28th, 2008, 4:41pm
 
In a program I'm writing, I need a function that creates a list of random numbers (from 0 to a number), and I don't know how to do it in processing.

Ex.:
int list = new int[num];
void createList( int num ) {
...
}

This function would create a list of numbers from 1 to num, in a randomic order:

void createList (6) --> the variable list would receive {0,1,2,5,4,3} or  {3,5,2,0,1,4}, something like that (I don't want repeated numbers)

It would be better if the list is an array, but it can be other structures, that's not a big deal.

It would be really helpful if someone could give some way to go.

Thanks for the help.
Re: list of random numbers
Reply #1 - May 28th, 2008, 5:09pm
 
Here is some code using Java Collections and the shuffle method :

Code:
int n = 10;
Integer[] myArray= new Integer[n];
for (int i = 0; i < myArray.length; i++) myArray[i] = new Integer(i);
java.util.Collections.shuffle(java.util.Arrays.asList(myArray));
println(myArray);


Be careful, myArray contains Integers, not ints (you can use the intValue() method to get the int from an Integer instance).
Re: list of random numbers
Reply #2 - May 28th, 2008, 5:21pm
 
Thanks for the answer... =)

It worked just fine... that's exactly what I need

Thanks again.
Page Index Toggle Pages: 1