mardi 5 janvier 2021

How can I generate random numbers without duplicates using Math.random()?

I am working on a program that will simulte a simplified version of Blackjack. Whenever someone draws a card, it can't be reused. Up to this point, I have learned about the Math.Random() function, so I am positive that I am supposed to use it in this assignment.

My first attempt was to create a boolean array of 52 (52 cards in a single deck) and use this line of code: int playerCard = (int) (Math.random() * 52);. I was going to manually go through all 52 possibilities and assign each of them to an index in the array. I realized this would take too much time and effort, especially because I have to do this multiple times in this assignment. It just wouldn't be efficient if everyone did this every time they wanted to generate random numbers without repeats.

Notes: If possible, please try not to use import java.util.Random;, but Scanner is ok. Also, if this changes anything, I am to put this in a for loop.




Aucun commentaire:

Enregistrer un commentaire