lundi 2 février 2015

Random Generator keeps returning 0

I'm making a 21 pick up game. The computer chooses 1 to 3 randomly using a random generator (not Math.random, because i need int's not doubles) and returns it to the second method. When I try to assign a variable to the random generator, it needs to be initialized. No big deal, i set the variable equal to 0, then when i return the variable to the method, it is equal to 0, even though the random set it to 1, 2, or 3. I'm new to this, any help is appreciated!


Here's the code:



//Declare variables
Random rannum = new Random();
int compchoice = 0;

//if statements containing computer moves
if(totalstone >=10)
{
compchoice = rannum.nextInt(3)+1;
}

else if ( totalstone <10)
{
compchoice = rannum.nextInt(2)+1;
}
else if(totalstone <=3)
{
compchoice = totalstone;
}

//return
return compchoice;




Aucun commentaire:

Enregistrer un commentaire