Random Number Generation without Repetition

I am trying to generate random numbers of a specified range (0-35, for example) without generating any number twice until all numbers have been generated once.

Thank you for any guidance.

Regards,

Daniel

1 Like
1 Like

Note specifically that both IntList and FloatList have shuffle() built-in. From your description it sounds like you want ints?

So one approach is to make an ordered list, shuffle it, use it until you are done with it, then get another one.

void setup() {
  for (int i=0; i<3; i++) {
    int[] nums = scramble(10);
    println(nums);
    println();
  }
}

int[] scramble (int count) {
  IntList scrambler = new IntList();
  for (int i=0; i<count; i++) {
    scrambler.append(i);
  }
  scrambler.shuffle();
  return scrambler.array();
}

Output:

Summary

[0] 7
[1] 5
[2] 2
[3] 6
[4] 8
[5] 0
[6] 4
[7] 3
[8] 9
[9] 1

[0] 1
[1] 8
[2] 7
[3] 5
[4] 3
[5] 9
[6] 2
[7] 4
[8] 0
[9] 6

[0] 2
[1] 1
[2] 3
[3] 6
[4] 9
[5] 7
[6] 4
[7] 8
[8] 0
[9] 5

1 Like

Can something like this be done with words or names, for instance, to generate a random name picker that does not repeat words or names???
Thanks!
r8th

Hello,

This should answer your question:
https://processing.org/reference/StringList.html

I also tried it.

:)

1 Like