1

I need a sort of algorithm or a library that can do this stuf:

I suppose to have an integer sequence of numbers that starts with 0 and ends with n number, such as:

0,1,2,3,4,5,6,7,8,9,10,11

I want to mess up this sequnce of numbers by a numeric key, so i use as key 378 and the algoritm give me this new sequence of numbers:

7,5,3,11,0,9,4,1,8,10,2

so my question is. There is a sort of algoritm or library that can do this in java?

Ric868
  • 89
  • 3
  • 4
  • 2
    what do you mean by this? you want to shuffle the list? – Ryuzaki L Sep 15 '18 at 14:42
  • i want this exactly! – Ric868 Sep 15 '18 at 14:44
  • I don't think is an exact duplicate, as the OP wants to pass in a custom source of randomness. – Jacob G. Sep 15 '18 at 14:45
  • @JacobG. You can seed the random number generator, for repeatable results. You can even supply your own implementation of `Random` if a specific randomization algorithm is desired. – Andreas Sep 15 '18 at 14:47
  • @Andreas Yes, that's what I specified in my answer. However, the duplicate question mentions nothing about that. It seems the duplicate of the duplicate mentions it, but in a different context. – Jacob G. Sep 15 '18 at 14:47

1 Answers1

1

Collections.shuffle allows you to pass in your own source of randomness, so you can use 378 as the seed and always receive the same shuffled list:

var numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);

Collections.shuffle(numbers, new Random(378));

System.out.println(numbers);

Output:

[4, 10, 3, 2, 0, 7, 9, 11, 5, 6, 1, 8]
Jacob G.
  • 28,856
  • 5
  • 62
  • 116