i want random numbers code that dont repeat

hi I use random numbers code javascript.i click on button I have a number.i want numbers dont repeat.for example I want click on button ,I have 4 ,on click 2,on click 1,onclick 3 and numbers don't repeat.

I guess, i need to create an array of the possible values and each time you retrieve a random index from the array to use one of the values, i remove it from the array?

2 Replies
Brian Dennis

Removing the entry from the array would be one way, though you risk running out of numbers. If you flip the logic and save numbers generated to an array your code would be stronger. Push the number generated to an array, then check the array each time you generate another random number. If found, try Math.random again