python\'s random


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.