I am working on a game. I want to do something like 50% of the time it shows you are lucky today here is $100 and 50% of the time it should say better luck next time. Can anyone please guide me how can i do this? I am using eclipse and java.
I have this so far:
for (int a = 0; a < 100; a++) {
int per = 0;
per = GenerateRandom.getRandom(2);
if (per<50)
{"better luck next time"}
else
{"you won $100"}
}
Is this right way to do? Or is there better way to do it?
First – no, that’s not quite the way to do it. You would want to make use of the
Randomclass in Android to generate pseudo-random numbers. (You can learn more about PRNGs here.)In my mind, the best way to do this (from a practical Java standpoint is to use code similar to the following: