dimanche 29 mars 2020

Java: Math.random() returning values beyond range [closed]

new user/programmer here. I'm having trouble with the Math.random() method returning values beyond the specified range. I'm using the method to generate a random card and I get output such as 99 of hearts which is invalid, as there are only 14 ranks. I've noticed that a lot of the ranks generated are multiples of 11 if that helps, no issues with suits so far. Keep in mind I'm new and still learning basic programming and the following program isn't finished, so simple explanations/help would be greatly appreciated. Also having trouble getting the entire program into the code window on here, sorry.

    int rank = 2 + (int)(Math.random() * 15);
    int suit = (int)(Math.random() * 4);  



Aucun commentaire:

Enregistrer un commentaire