How can I generate random numbers without repeating them? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I generate random numbers without repeating them?

I want to generate random numbers between 1 and 100, but I want the generated number to be different from all the previous generated numbers. How can I do it? Help me please

6th Jun 2018, 1:16 AM
Edgar Oviedo
Edgar Oviedo - avatar
4 Answers
+ 3
you can take one array and put randomize number you get. Now when you get next number you can first check to the array if it is already present then neglect that and get new random number. follow same thing up to array size will become 100.
6th Jun 2018, 1:50 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 3
nums.indexOf(num)!=-1 // is to make sure the random number is not repected in the array nums.push(num) // is to update the array with the random number num
6th Jun 2018, 2:43 AM
Calviղ
Calviղ - avatar
0
thanks guys :D
6th Jun 2018, 2:09 AM
Edgar Oviedo
Edgar Oviedo - avatar