JS Project help needed: duplicate, unordered numbers

Project Name: Mega Billions( Clone of Megamillions )

Project Link: https://megabillions.ga/

Source-code link: ryanbest99.github.io/mega-billions at master · ryanbest99/ryanbest99.github.io · GitHub

[Problems I want to solve]

  1. Each white ball number MUST be unique (no duplicates)
  2. List the numbers in order…

[Description]

Hey guys, I’m trying to make a mega-million clone web service using JS.

To pick the random number for each ball, I used ‘Math.random()’ on each ball. (White balls: 1-70 / Yellow-ball: 1-26)

However, it causes two issues that I don’t know how to solve.

  1. Currently, the numbers can be duplicated ( like 26, 26, 26, 26, 27, 21)
  2. The numbers are not in numerical order.

Thank you in advance for your help!

Problem number 2 is easily solved with Array.sort().

For problem number 1, what I’d do is I’d put all the possible numbers in an array, shuffle the array then take the number of items you need from the start of the array. Unfortunately Array doesn’t come with a shuffle function so you’ll have to write it yourself: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
Note that it’s easy to make it non-random if you don’t implement it exactly.

@sfiquet
Thank you for suggesting a new solution. I need to study Array.sort() and try it. Thx again!

@camperextraordinaire @sfiquet

Guys, thanks to you, I finally did it !!!
Thanks again for your help!!!

Complete project link: https://megabillions.ga/

Source-code link: