Unique Random Number for serial music

edited October 2014 in How To...

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.

  • edited October 2014

    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.

  • I think this is a very precise question.

    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.

  • edited October 2014

    If you have a large range of possible values and only need a small number of values then this would do what you want

    import java.util.*;
    
    // Define the smallest and largest values that can be used for the number
    URN urn = new URN(100, 199);
    
    void setup() {
      for (int i = 0; i < 10; i++)
        println(urn.get());
    }
    
    class URN {
    
      Set<Integer> used = new HashSet<Integer>();
      Integer min = 0;
      Integer max = 999;
    
      URN(int minValue, int maxValue) {
        min = minValue;
        max = maxValue;
      }
    
      // Return a random number from the range that hasn't been used before
      int get() {
        int nbr;
        do {
         nbr = (int)random(min, max + 1);
        } while(used.contains(nbr));
        used.add(nbr);
        return nbr;
      }
    }
    

    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:

    import java.util.UUID;
    
    for (int i = 0; i < 10; i++)
    {
      println(UUID.randomUUID());
    }
    

    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.

  • _vk_vk
    Answer ✓

    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:

    int range = 12;
    int [] numbers = new int[range+1];
    int a;
    
    
    
    void setup() {
      for (int  i = 0; i < numbers.length; i++) {
        numbers[i] = i ;
      }
      shuff(numbers);
      println(numbers);
    }
    
    void draw(){}
    
    void mouseClicked(){
      println(numbers[a]);
      a = (a + 1)%numbers.length;
    }
    
    
    void shuff(int[] array) {
      // i is the number of items remaining to be shuffled.
      for (int i = array.length; i > 1; i--) {
        // Pick a random element to swap with the i-th element.
        int j = int(random(i));  // 0 <= j <= i-1 (0-based array)
        // Swap array elements.
        int tmp = array[j];
        array[j] = array[i-1];
        array[i-1] = tmp;
      }
    }
    
  • 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?

    int range = 12;
    ArrayList numbers = new ArrayList(range);
    int a;
    
    void setup() {
      for (int i = 0 ; i < range ; i++) {
        numbers.add(i);
      }
      Collections.shuffle(numbers);
      println(numbers);
    }
    
    void draw() {
    }
    
    void mouseClicked(){
      println(numbers.get(a));
      a = (a + 1) % numbers.size();
    }
    
  • edited October 2014

    @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

    // forum.processing.org/two/discussion/7688/
    // unique-random-number-for-serial-music
    
    final int RANGE = 12;
    final IntList numbers = IntList.fromRange(RANGE);
    
    println(numbers);
    numbers.shuffle(this);
    println(numbers);
    
    exit();
    
  • _vk_vk
    edited October 2014

    @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

Sign In or Register to comment.