User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,755 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,503 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Jul 5th, 2005
Views: 5,708
Python uses the Mersenne Twister as the core random generator. The module random contains a fair number of methods (or functions) that help you to do random things. Here are some examples ...
Last edited : Sep 29th, 2006.
python Syntax | 5 stars
  1. # doing random things
  2. # tested with Python24 vegaseat 04jul2005
  3.  
  4. import random
  5.  
  6. print "List the functions/methods and constants of module random:"
  7. for funk in dir(random):
  8. print funk
  9.  
  10. print
  11.  
  12. print "more info on seed():"
  13. help('random.seed')
  14.  
  15. print
  16.  
  17. # seed the random generator (default from system time)
  18. random.seed()
  19.  
  20. # floating point random numbers are 0 to < 1.0
  21. print "Display a couple of random floating point numbers:"
  22. for k in range(10):
  23. print random.random()
  24.  
  25. print
  26.  
  27. print "Pick a floating point number in the range 1.0 to < 100.0:"
  28. print random.uniform(1.0, 100.0)
  29.  
  30. print
  31.  
  32. print "Pick a random integer 1, 2 or 3:"
  33. for k in range(10):
  34. r = random.randint(1,3)
  35. print r,
  36.  
  37. print
  38.  
  39. print "\nPick a random integer in the range 0 to 9:"
  40. randomInt = random.randrange(10)
  41. print randomInt
  42.  
  43. print
  44.  
  45. print "Pick an even random integer in the range 10 to 99:"
  46. randomInt = random.randrange(10, 100, 2)
  47. print randomInt
  48.  
  49. print
  50.  
  51. print "Create a list of 10 unique random integers in the range 0 to 99:"
  52. randomIntList = random.sample(xrange(100), 10)
  53. print randomIntList
  54.  
  55. print
  56.  
  57. str1 = "If you throw it away, you will need it the next day."
  58. print str1
  59. print "Two random characters picked from the string above =", random.sample(str1, 2)
  60.  
  61. print
  62.  
  63. # strings are immutable (cannot be directly changed), so you have to go to a list
  64. # of characters, do the operation, and join the changed list back to a string
  65. str1 = "Mississippi"
  66. charList = list(str1)
  67. random.shuffle(charList)
  68. str2 = "".join(charList)
  69. print "String '%s' after random shuffle = '%s'" % (str1, str2)
  70.  
  71. print
  72.  
  73. print "Pick a fruit at random from a list of fruits:"
  74. fruitList = ['apple', 'pear', 'banana', 'grape', 'cherry']
  75. print fruitList
  76. print "Picked", random.choice(fruitList)
  77.  
  78. print
  79.  
  80. # coin toss ...
  81. count = 0
  82. toss = int(raw_input("Enter number of tosses: "))
  83. while (count < toss):
  84. print random.choice(('head', 'tail'))
  85. count += 1
  86.  
  87. print
  88.  
  89. # random fun, add this to your sober program to lighten up the day
  90. oldList = []
  91. oldList.append('Old Swimmers have one more stroke!')
  92. oldList.append('Old Plumbers go down the drain!')
  93. oldList.append('Old Exorcists give up the ghost!')
  94. oldList.append('Old Ministers go out to pastor!')
  95. oldList.append('Old Composers decompose!')
  96. oldList.append('Old Golfers lose their balls!')
  97. oldList.append('Old Fishermen have limp rods!')
  98. oldList.append('Old Farmers go to seed!')
  99. oldList.append('Old Editors do it with a red pen!')
  100. oldList.append('Old Eskimoes get cold feet!')
  101. oldList.append('Old Accountants lose their balance!')
  102. print random.choice(oldList)
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 3:38 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC