-1

I have a code in Python where at different time steps I'm following a number of different steps. What I want to do is add an extra part in the code where in the first 7 time steps the algorithm will choose randomly a number from an existing set that has 7 different entries (0,1,2,3,4,5,6,7). Essentially what I want to do is choosing without replacement from this set so that at t=7 all the numbers have been chosen once. So at t=0 the set that I need to choose a random element from has 7 elements, at t=1 it has 6 elements etc. Any help would be very much appreciated.

Andriana
  • 367
  • 3
  • 8
  • 17
  • 3
    You can shuffle the list of numbers and then just loop over the shuffled list. Try writing some code that does that, and if you get stuck, post it here and explain clearly where you're stuck. – PM 2Ring Apr 30 '18 at 11:05

1 Answers1

0

It really depends what you are doing as to how you complete it. If you are looking to simply select 7 indexes in random order, you can create a list, and a small function to pop elements off that list, taking into account the current size of the list.

import random

alist = [0,1,2,3,4,5,6]
while len(alist) > 0:
    alist.pop(random.randrange(0,len(alist)))

If you like, what you could do is append each element you pop off the list to another, to then use as an input for your function.

All of this said, without slightly more context, it's touch to know if this answers your question! Hope it helps all the same!

rtob
  • 146
  • 1
  • 10