random - Java RandomGenerator no reponse to be repeated twice in a row -
i need output random number relate repose. pick 1 need generate random number no number repeated twice in row. know have store previous value not know start. here code.
private string pickdefaultresponse() { // pick random number index in default response list. // number between 0 (inclusive) , size of list (exclusive). int previndex; int newindex; int index = randomgenerator.nextint(defaultresponses.size()); return defaultresponses.get(index); }
previndex must not inside method, keep @ class level.
private int previndex = -1; private string pickdefaultresponse() { int index = 0; { index = randomgenerator.nextint(defaultresponses.size()); } while( index == previndex ); previndex = index; return defaultresponses.get(index); }
Comments
Post a Comment