mercredi 21 juin 2017

Android random - not quite random with 2 arrays

I want to pass my Random, depending on the users input, a variable amount of Array's. Out of which the Randomizer should choose 1 line and a new one every time a button is pressed.

However, what I noticed is that the random first choose is between the given Arrays (which all contain different number of Lines), before picking a Line inside that Array. If one of the Arrays only contains a very small amount of Lines, it's likely that the Randomizer will return the same Line multiple times back-to-back. Instead of choosing between all lines in the given Arrays.

Is there a way to fix this issue?

int[] Options = {
      modeID, 
      sideID,
      //etc...
};

Random random = new Random();
int randomIndex = random.nextInt(Options.length);

String[] Output = getResources().getStringArray(Options[randomIndex]);
String randString = Output[random.nextInt(Output.length)];
random.setText(randString);




Aucun commentaire:

Enregistrer un commentaire