We are about to switch to a new forum software. Until then we have removed the registration on this forum.
it seems to me very odd that Processing does not include a function to generate urn. In music computing such function is kind of vital if one needs to approach serial thinking.
I need a random to generate 12 unique random numbers.. so far I figured out that maybe I need to create a function using an array, but I can't figure out how to delete a number from an array once it is used.
any help is very appreciated.
Answers
It's tiresome keep telling this all the time... But posting code attempts helps us immensely pinpointing what's exactly being asked! We help solving bugs, not make the whole code for ya!
I don't have any code to paste here so far, otherwise I would have pasted it here. I just need a urn function that outputs random numbers from 1 to 12, uniquely. I think this is a very precise question.
I'm not asking u to code all my work. I just need a function that it should be really standard as random or noise-.. this lack is very odd.
but the original didn't mention the range. and this additional information helps immensely.
anyway, the java Collections.shuffle(list) method will randomly arrange the passed-in list.
http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)
really does the range make any difference? by the way thanks
knowing it's a small, contiguous range means we can use a shuffle algorithm, fisher-yates / knuth, standard stuff, in fact it's part of the standard java library.
picking 12 out of a range of 1,000,000 or more would use a different algorithm, one we'd probably have to code ourselves. (store choices, pick random number, check previous choices, repeat if duplicate). would be a different algorithm again for choosing 100000 out of 100000.
If you have a large range of possible values and only need a small number of values then this would do what you want
I should have read the question more closely - this is OTT but I leave it here because others might find it useful.
At work, when we want IDs truly unique, whatever the time where we draw them, we use UUIDs:
You can get longs out of them, for example: uuid.getLeastSignificantBits()
But as said, it can be a bit overkill, it depends on your needs, the range needed, etc.
When I faced this question I end up making an array, filling with the numbers of the range, shuffle it, and then just get the numbers out of the array in order.
something like:
thanks this last one is very interesting. I'll sit down and carefully study it.
thanks everybody for the kind help
or you can just use Collections.shuffle()
the java standard version has been used, and therefore tested, by literally thousands of people. your version hasn't. why re-invent the wheel?
@koogs, don't forget that Processing's IDE doesn't automatically
import
missing libraries:import java.util.Collections;
And it's pretty advisable to specify the datatype reference a Collection gonna hold:
ArrayList<Integer> numbers = new ArrayList(12);
Anyways, here's a shorter alternative version w/ IntList instead: :ar!
http://processing.org/reference/IntList.html
@koogs That's right, I just used this because it was in hand, in another IDE tab, I'm using it in another sketch to keep it js compatible. This is supposed to be an implementation of Fisher-Yates shuffle...
@GoToLoop I keep forgetting about intList...
"why re-invent the wheel?"
Because if you make a very large list, there is a big overhead, in memory and CPU, to box / unbox all these integers into Integers...
http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array