I've been making a little game in Java and I'm using the Java Random class to generate random number.
During the course of the game, two teams play one another, and to figure out who wins the match, I generate two random numbers (from 1 to 10, like a d10) and then do some modifications to those scores, comparing them to find out who wins.
But I was wondering if there was a more efficient way to do it. I don't really know exactly how the Java Random class works, but my idea was that I would instead generate a single random number (from 1 to 100) and then integer divide by 10 to get one of the numbers and then do mod 10 to find the other number.
In code, my question is which of these is faster:
Random r = new Random();
int team1Score;
int team2Score;
int randNum;
// current version
team1Score = r.nextInt(10) + 1;
team2Score = r.nextInt(10) + 1;
// new version
randNum = r.nextInt(100) + 1;
team1Score = randNum / 10;
team2Score = randNum % 10;
And if, anyone has any ideas, is there any way to make it even more efficient than either of these?
Aucun commentaire:
Enregistrer un commentaire