1

my question is pretty simple in the concept : I have an array of 24 booleans which I've set all to false initially. I can choose randomly an element of this array with the code:

Random r = new Random();
int i1=r.nextInt(24);

Now from time to time I change the value of some elements to true, let say to the element number 14 and 22. How can I choose now randomly only between the other elements who are still false and not consider the one who are true? Thank you in advance for the answer.

Edit : The code who works was :

Random r = new Random ();
int rand = r.nextInt (24);

while (array[rand]) {
          rand = r.nextInt (24);

        }
Ardi
  • 23
  • 4

3 Answers3

0

You can keep track of the indices in which the elements have the value of true in an ArrayList for example. When generating a random number, check whether it's equal to one of the already true numbers (which should be excluded from the random number generation/selection) and re-generate a random number, until you get one of the array indices which is still at false.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • yea but the problem is that when the initial array (the one from which I want to random) will be filled with true elements at 23 of 24 total elements , in this case I'll random for 23 times till I find the one who is still false right? Just want something more efficient but if I'll not find something else than that will be the solution – Ardi Feb 21 '14 at 00:41
0
int numberTrue=0; //adjust this in your other code accordingly
Random r = new Random ();
int rand = r.nextInt (24);
// loop until not true found or stop if all items are true
while (array [rand] && numberTrue < 24) {
  rand = r.nextInt (24);
  count++;
}

I thought about it for a bit, and without knowing your code here are a couple of suggestions to avoid that infinite loop when they are all true (assuming that condition may exist):
1. keep a count of how many true are set (example adjusted for this)
2. Store a second array of the true indexes, as suggested in the other answer

The first is probably the simplest to implement (adding a numberTrue++; and numberTrue--; in the appropriate code spots) - just a note here that if it's not possible to keep this count, you can achieve the same thing by running an initial for loop over your array and counting the number of true elements.
The second way would lend itself to utilizing one of the answers from the other post:

public int generateRandom(int start, int end, ArrayList<Integer> excludeRows) {
    Random rand = new Random();
    int range = end - start + 1;

    int random = rand.nextInt(range) + 1;
    while(excludeRows.contains(random)) {
        random = rand.nextInt(range) + 1;
    }

    return random;
}

(I believe their code should actually read rand.nextInt(range) + start
So you would run:

if (trueIndexes.length < 24) {
      generateRandom(0, 23, trueIndexes);
    }
Tim
  • 491
  • 3
  • 6
  • also, if you want to try for efficiency then this question on excluding some numbers from the Random might help you: http://stackoverflow.com/questions/14935997/generate-random-numbers-except-certain-values – Tim Feb 21 '14 at 00:54
  • yea I think I'll use that while loop. Now I'm looking the link, it seems useful, thanks – Ardi Feb 21 '14 at 01:00
  • I'm re-looking the code and maybe I don't understend well but it seems to me that it try for 24 times to generate that number.. well if I have 1 chance on 24 to generate the desired number it dont mean that if I try 24 time I'll surely generate it...it may require 5 time or 20 or 30 or 100 or more time because is probabilistic – Ardi Feb 21 '14 at 01:15
  • Yep, I saw that too - so answer adjusted. I thought of a couple of ways (other than initially running -another- for loop over the array) to determine when to stop – Tim Feb 21 '14 at 01:28
0

You can create list of boolean elements which have "false" and choose randomly elements of this list.