How to create a random with no repeat (2 values in a row)?

Hi there,

I have a random to create between 1 and 3 but people can retry and it cant repeat. For example we have 1,2,3 : at first random will choose value “1” then retry and pick “2” then retry and pick “3”.

So the new number will never be the same as the first and the second number. I already try to do it with the no repeat patch on the library but it’s not working.

Does anyone have an idea on how to create something like this ?

Thanks a lot & have a great day !

Maybe it could be done in patches but I think it will be much simpler in script.


var Patches = require('Patches')
var values = [1, 2, 3, 4, 5]

function shuffle(array) {
  let currentIndex = array.length, 
    randomIndex;
  while (currentIndex != 0) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--
    [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]
  }
  return array
}
shuffle(values)
var requestValuePulse = await Patches.outputs.getPulse('requestValuePulse')

requestValuePulse.monitor().subscribe(() => {
  // pop the value off the array so it can't be picked again
  Patches.inputs.setScalar('result', values.pop())
})

Hi @josh_beckwith,

Thanks for your help ! The console is saying there is a problem with the async function, is this possible for you to make it work in a simple and basic project for me ? So I can see how I should do !?

Thanks a lot again !
JBM

If you create a new script you can see how to define an async block (which is required to use await).

It looks something like this:

(async function(){
  // your code goes here
})()