Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Konstantinos Aggelidis
Konstantinos Aggelidis
4,881 Points

How to create 5 random numbers but in each step, exclude the previous results?

Hi, I am trying to write a program that will generate 5 random numbers, but in each step, I would like to exclude the previous value from the next possible values. In short, if I generate 5 random numbers, I do not want to have any duplicates. Then I am trying to have the computer guess 5 numbers, and see how many times it took to actually hit all 5 of them correctly.

I wrote a program without the above restriction, but I cannot get how I can limit the results to exclude the previously guessed or generated values (unless in each step I put an if statement, witch will have additional restrictions in each step). Maybe an approach with a list would be more suitable?

var upper = 40;

var randomNumberA = getRandomNumber(upper); var randomNumberB = getRandomNumber(upper); var randomNumberC = getRandomNumber(upper); var randomNumberD = getRandomNumber(upper); var randomNumberE = getRandomNumber(upper);

var guessA; var guessB; var guessC; var guessD; var guessE;

var attempts = 0;

function getRandomNumber (upper) { return Math.floor (Math.random() * upper) +1; }

while (randomNumberA !== guessA || randomNumberB !== guessB || randomNumberC !== guessC || randomNumberD !== guessD || randomNumberE !== guessE ) {

guessA = getRandomNumber(upper); guessB = getRandomNumber(upper); guessC = getRandomNumber(upper); guessD = getRandomNumber(upper); guessE = getRandomNumber(upper);

attempts += 1; }

document.write("<p>The random numbers were: " + randomNumberA + " " + randomNumberB + " "+ randomNumberC + " " + randomNumberD + " " + randomNumberE + " " + "<p>");

document.write("It took the computer " + attempts + " attempts to get it rights.</p>");

1 Answer

Steven Parker
Steven Parker
229,004 Points

One good way to prevent duplication is to pick from a discrete set of values, and remove the picked value each time.

  • create an array containing all possible values
  • randomly pick one value from the array
  • "splice" the array to remove the picked value
  • repeat as many times as desired, picking from the increasingly smaller array

This strategy works well for numbers, quiz questions, quotes, or any other situation where you want random choices without repeats.