1

I try to constantly show one of 5 possible items randomly (for a 100 times or so), BUT the important thing is that the SAME item is not allowed to be shown immediately after each other. There needs to be (at least) always one other item in between.

Any idea?

Thank you soo much

1
  • 1
    pick random item, if same as last picked item, pick another random item. lather, rinse, repeat. Commented Aug 29, 2013 at 15:38

5 Answers 5

1

Please see the sample code below.

 import random import sys def special_select(d, num_to_show=100): selected = None for i in range(num_to_show): selected = random.choice(list(d.difference([selected]))) print selected 

You may try the function like follows.

 >> d = set(range(5)) >> special_select(d) 
Sign up to request clarification or add additional context in comments.

1 Comment

thank you all so much. I will try to implement it in my script. .fingers crossed... Thank you so much!
0

I'd go for:

from itertools import groupby, islice from random import choice choices = range(5) items = (k for k, g in groupby(iter(lambda: choice(choices), None))) for item in islice(items, 100): print item 

This lets itertools.groupby handle consecutive duplicate values and islice means you can just skim some off, so if you wanted 50 of them, just use:

random50 = list(islice(items, 50)) 

Comments

0

Remember the previous item and keep picking a new choice until it differs:

import random def pick_random(options): last = next = None while True: while next == last: next = random.choice(options) yield next last = next 

Use this as a generator:

>>> options = (1, 2, 3, 4, 5) >>> picks = pick_random(options) >>> for i in range(100): ... print(next(picks)) ... 1 5 4 3 2 3 (...) 3 4 5 

Using itertools.islice() makes it easier to pick just 100:

from itertools import islice for pick in islice(pick_random(options), 100): print(pick) 

11 Comments

this doesn't guarantee you don't repeat the last pick from a call with the first pick of the next call.
@PaulEvans: I discovered that, yes, so I changed tack.
Doesn't work because the last element in one random.sample can be the same as the first in the next. Good try, though.
@MartijnPieters Now that's much more complicated than the algorithm I proposed.
@MartijnPieters Incorrect. Didn't notice your change of method. Now it implements the algorithm I proposed to the OP. We should stop doing their homework, though.
|
0

Not the most efficient one in Python, but what is done here is: when append the next item, the next item is sampled from the pool excluding the previous item.

def rand_chain(n, chos_from): rd_ls=[] rd_ls.append(random.choice(chos_from)) for i in range(n-1): rd_ls.append(random.choice([v for v in chos_from if v!=rd_ls[i]])) return rd_ls >>> rand_chain(10, range(10)) [8, 2, 9, 2, 9, 5, 9, 8, 2, 4] 

If you use numpy, replace [v for v in chos_from if v!=rd_ls[i]] with more efficient stuff.

Comments

0

Yes. Plenty.

  • Determine number of times to iterate.
  • Start a random number generator.
  • Set variable previous to None or similar.
  • Start iteration.
  • Generate next random number and compare it with previous. If it is the same, skip it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.