Posted By

magicrebirth on 03/09/10


Tagged

textmate python random sample shuffle Choice


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

hebertialmeida
gartenstuhl


python's random


 / Published in: Python
 

  1. from random import *
  2.  
  3.  
  4. for x in range(10):
  5. print random()
  6.  
  7.  
  8. print '============================='
  9.  
  10. z = ['ciao', 'piccolo', 'bambino', 'sono', 'qua', 'io', 'in verita']
  11.  
  12. for x in z:
  13. # picks a random el from a list
  14. print choice(z)
  15.  
  16.  
  17. print '============================='
  18.  
  19. # "Two ways to randomly attach elements"
  20.  
  21.  
  22. for x in range(10):
  23. shuffle(z) # shuffle modifies the element in place so you have to call it beforehand
  24. print " ".join(z)
  25.  
  26.  
  27. # an alternative method
  28. print '============================='
  29.  
  30. for x in range(10):
  31. print " ".join(sample(z, len(z)))

Report this snippet  

You need to login to post a comment.