How to randomize number without repitition within a range | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to randomize number without repitition within a range

For example: The range is 20 There's a button which randomizes a number. It should result to a 20 random numbers from 1 to 20 without repeating if I clicked the button 20 times. Program to use: JavaScript or jQuery https://code.sololearn.com/W0g2I7uDP58v/?ref=app

20th Aug 2018, 10:47 AM
Email Not Activated
1 Answer
+ 3
function randomNumbersArray(n) { var nums = [], num; for (var i = 0; i < n; i++) { do { num = (Math.floor(Math.random() * n)); } while (nums.indexOf(num)!=-1) nums.push(num); } return nums; } var randomNumber20 = randomNumbersArray(20); https://code.sololearn.com/W7Lm9Ufx4cna/?ref=app
20th Aug 2018, 11:41 AM
Calviղ
Calviղ - avatar