Hi...

I've this:

random_name = ['Mohamed', 'Ahmed','Aboubakr']
print "1: %s    2: %s    3: %s" % (random_name[1], random_name[0], random_name[2])

I want to print all the three names but in random way,

as: some time: Mohamed, Ahmed, Aboubaker

and some time: Mohamed, Aboubaker, Ahmed

and some time: Aboubaker, Ahmed, Mohamed

and some time ...

I used random and it doesn't work correctely !!

Recommended Answers

All 4 Replies

You are looking wrong place. Check itertools.permutations and random.choise

You are looking for random.shuffle().
To see all method under random module use dir(random)
For help use help(random.shuffle)

>>> random_name = ['Mohamed', 'Ahmed','Aboubakr']
>>> random.shuffle(random_name)
>>> random_name
['Mohamed', 'Aboubakr', 'Ahmed']
>>> random.shuffle(random_name)
>>> random_name
['Ahmed', 'Mohamed', 'Aboubakr']
>>> random.shuffle(random_name)
>>> random_name
['Ahmed', 'Aboubakr', 'Mohamed']
>>>
commented: thanks +13

Thank's :)

If you got your reply, it is your duty as OP (original poster) to close the thread as solved and give any reputation for excellent answer(s).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.