0

I have a set oneHundredInactiveSynapses and I would like to manipulate a random 21 of the 100 inactiveSynapses in that set and then stop. I feel like there should be a easy way to do this, but I just can't think of anything right now.

for (Synapse inactiveSynapse : oneHundredInactiveSynapses) { // I want to call a method on the inactiveSynapse like inactiveSynapse.setActiveState(true); // but I only want to do this to 21 of the synapses. How can I do this? } 

Other points:

  1. making oneHundredInactiveSynapses into an array is not an option
  2. I don't care which 21 become active
5
  • 1
    Do you actually mean random, or do you mean you don't care which 21? Commented Jun 18, 2013 at 22:56
  • I don't care which 21. Commented Jun 18, 2013 at 22:59
  • @LouisWasserman Two very different things... Commented Jun 18, 2013 at 22:59
  • 1
    Just grab the first 21 elements. Use your for loop with a counter that counts up to 21. Commented Jun 18, 2013 at 23:04
  • Out of interest, why can you not create a shallow array/list copy of your collection? It wouldn't take much memory. Commented Jun 18, 2013 at 23:15

4 Answers 4

1

You can make a copy of the Set, shuffle and get the sub-list of first 21 elements -

List<Synapse> copy = new ArrayList<Synapse>(original); Collections.shuffle(copy); List<Synapse> sub = copy.subList(0, 21); for(Synapse s : sub) { ... } 
Sign up to request clarification or add additional context in comments.

1 Comment

IMHO, one hundred is not considered huge. I had thought about it but the OP has explicitly mentioned 100 and 21 out of 100.
0

To make it a little random you could randomly choose ones to fire until you reach 21. Note that if you only have enough left to reach 21 you'll have to fire all of those so there is a bit of bias towards the synapses at the end.

 int chosen = 0; int left = oneHundredInactiveSynapses.size(); java.util.Random rand = new java.util.Random(); for (Synapse inactiveSynapse : oneHundredInactiveSynapses) { if(left-chosen>21) inactiveSynapse.setActiveState(true); else{ if(rand.nextBoolean()){ inactiveSynapse.setActiveState(true); chosen++; if(chosen==21) break; } } left--; } 

But if you really don't care about randomness then just pick the first 21 using a count i.e.

 int count = 0; for(Synapse inactiveSynapse : oneHundredInactiveSynapses){ if(count==21) break; inactiveSynapse.setActiveState(true); count++; } 

Comments

0

Convert the Set to an array, then access 21 random elements of the array. Your question says the set has only 100 elements so that should not be too expensive. Something along those lines is the only option if you can not select the concrete Set implementation or alter the code populating the set.

If you can alter the code populating the set, you could of course just have it stop once it added 21 elements. If you can inject your own Set implementation, you could have an implementation that provides a random ordering, and iterate over only the first 21 elements.

3 Comments

This can have adverse affects when dealing with large amounts of data; Set can and usually is much more efficient. That said, it's a good enough solution as a proof-of-concept.
Also, making it into an array is not an option.
@628496 Perhaps you should edit that into your Q; it's a pretty stand-by practice.
0

If you are okay with the first 21 you can do:

int count = 0 for (Synapse inactiveSynapse : oneHundredInactiveSynapses) { if(count < 21){ inactiveSynapse.setActiveState(true); count++; }else{ break; } } 

If you want random you could do:

java.util.Random rand = new java.util.Random(); int count = 0 for (Synapse inactiveSynapse : oneHundredInactiveSynapses) { if(rand.nextInt(100) % 2){ next; } if(count < 21){ inactiveSynapse.setActiveState(true); count++; }else{ break; } 

This would go to the next element if the random number is even, until you get to 21, which would break. }

3 Comments

you should just use rand.nextBoolean instead of creating an int and checking if it's even.
I suppose that is another way of doing it. It's going to have the same run time regardless.
If that random generator generates 100 odd numbers, it won't activate any synapses! There are better ways of doing this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.