23

I'm doing this for a school project (so I can't use any advanced features) and I'm using Python 2.6.6.

I have a list of numbers from 1 to 1000 and a seed (448 for example).

How can I generate a random sequence with that seed so that the numbers in my list will be in a different index ?

And is it possible, knowing the seed, to return the list in its original order ?

3
  • 1
    docs.python.org/release/2.6.6/library/random.html will tell you everything you need to know Commented Dec 29, 2010 at 20:12
  • @Rafe I've read that before but didn't understand Commented Dec 29, 2010 at 20:54
  • I really hope that 448 just happened to be a redicoulsly small number. For your input, there are factorial(1000) sort options, so in order to get proper psaudo-random you will need a seed of this magnitude. Commented Dec 27, 2018 at 0:52

2 Answers 2

53
import random
SEED = 448

myList = [ 'list', 'elements', 'go', 'here' ]
random.seed(SEED)
random.shuffle(myList)

print myList

results in

['here', 'go', 'list', 'elements']

Your list is now pseudorandomized.

'Pseudo' is important, because all lists having the same seed and number of items will return in the same 'random' order. We can use this to un-shuffle your list; if it were truly random, this would be impossible.

Order = list(range(len(myList)))
# Order is a list having the same number of items as myList,
# where each position's value equals its index

random.seed(SEED)
random.shuffle(Order)
# Order is now shuffled in the same order as myList;
# so each position's value equals its original index

originalList = [0]*len(myList)   # empty list, but the right length
for index,originalIndex in enumerate(Order):
    originalList[originalIndex] = myList[index]
    # copy each item back to its original index

print originalList

results in

['list', 'elements', 'go', 'here']

Tada! originalList is now the original ordering of myList.

Sign up to request clarification or add additional context in comments.

2 Comments

Late, but this is a fantastic answer. Cool use of pseudorandomness!
Would be more fantastic if it created a local random.Random instance to seed instead of using the default one. This answer made me search for how to do it in a thread-safe manner.
3

A simple check on the python docs http://docs.python.org/library/random.html tells you about

random.seed([x])

which you can use to initialize the seed.

To get the items in the order of your initial again, set the seed again and get the random numbers again. You can then use this index to get the content in the list or just use the index for whatever.

You’d just sort the list and it’d be in sorted order again.

2 Comments

I've read that before but didn't understand. I've done random.seed(448) and it gives me nothing. How can i use this whitm my list?
You set the seed with that seed function. You then call another method in that module to get your next random number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.