Melony Smith
Melony Smith

Reputation: 23

JavaScript Random Number Generator

First time posting here. Been reading for a bit, though. I am unable to find exactly what I need. Apologies in advance if I overlooked something.

I have an assignment where I need to create a random number generator that generates 6 random numbers between 1 and 59 for a lottery. The only part I am having trouble with is coming up with the code to check for and prevent duplicate numbers from appearing. I am only allowed to use loops and arrays in this, which is why I am having such a hard time. I know it should be easy, but I am a true beginner at JavaScript and am completely at a loss here.

What do I need to do to make sure duplicate numbers are not used in the output? Again, I can only use loops and arrays, and everything is very "basic".

var powerballLotteryRandom;

var powerballRandom;

function genRanPowerballNum(min, max, num) {

        var powerballArray = [];

        for (var i = 0; i < num; i++) {

            var ranNum = Math.random() * (max - min) + min;

            powerballArray[i] = Math.round(ranNum);

        }

        return powerballArray;

    }

powerballLotteryRandom = genRanPowerballNum(1, 59, 5);

powerballRandom = genRanPowerballNum(1, 35, 1);

if (lotteryChoice === "powerball") {

    console.log("Your Powerball lottery numbers are " + powerballLotteryRandom + ", and your Powerball number is " + powerballRandom + ".");

What do I need to do, simply, to prevent duplicate numbers from showing up? Again, I can only use loops and arrays, I must use functions, and it should be kept very "basic", if that makes sense.

Hopefully I posted all of the code you might need. The generator actually firsts prompts users to enter whether they would like state or Powerball lottery numbers and then runs code according to what they picked. I would be happy to post more code if it is needed.

Thanks for any help! Melony

Upvotes: 2

Views: 793

Answers (1)

AmmarCSE
AmmarCSE

Reputation: 30607

  1. Use a while loop to keep iterating until a non-duplicate has been determined.
  2. Use indexOf() to determine whether ranNum is a duplicate by seeing if exists in the array or not. Note, indexOf() returns -1 if it does not exist in the array.
  3. After the while loop is done, set the array value (At this point, it should be a guaranteed unique)

var ranNum = Math.random() * (max - min) + min;

while (powerballArray.indexOf(Math.round(ranNum)) > -1) {
    ranNum = Math.random() * (max - min) + min;
}

powerballArray[i] = Math.round(ranNum);

var powerballLotteryRandom;

var powerballRandom;

function genRanPowerballNum(min, max, num) {

  var powerballArray = [];

  for (var i = 0; i < num; i++) {

    var ranNum = Math.random() * (max - min) + min;

    while (powerballArray.indexOf(Math.round(ranNum)) > -1) {
      ranNum = Math.random() * (max - min) + min;

    }

    powerballArray[i] = Math.round(ranNum);

  }

  return powerballArray;

}

powerballLotteryRandom = genRanPowerballNum(1, 59, 5);

powerballRandom = genRanPowerballNum(1, 35, 1);


console.log("Your Powerball lottery numbers are " + powerballLotteryRandom + ", and your Powerball number is " + powerballRandom + ".");

Upvotes: 2

Related Questions