My python program/function!

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 233
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: My python program/function!

 
1
  #11
Sep 8th, 2009
Originally Posted by paulthom12345 View Post
  1. import random, string
  2. print(''.join([random.choice(string.ascii_letters+"123456789") for f in range(8)]))
It's shorter, but the result is different: the letters and digits are not chosen with the same probability law. With your code, each letter or digit has the same probability to be chosen. AutoPython's method selects first the set of letters or the set of digits and then a random char in the chosen set. This increases the probability of digits because there are only 10 digits and 52 letters. The probability of a digit is about 5 times that of a letter.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 14
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Re: My python program/function!

 
0
  #12
Sep 8th, 2009
I learn yet another useful function. But before I post the code. I need to say that, no, the first global statement was necessary, and when I removed it said "global name dig_let undefined". Of course that is when I attempted to use it outside the function. However, I do believe you could get around that by using the return function, however though, I do not know how to use that.
@gribulous
I never thought about probability, but as a result, I simply add 4 other digit variables. So now the probability difference is that less then 0.10
Here's what I got so far:

  1. from random import choice, randint, shuffle
  2. from string import ascii_letters, digits
  3.  
  4. def randstr():
  5.  
  6. #Generates a random string.
  7. #String consists of numbers 0-9, letters a-z & A-Z.
  8. #Letters and number can be in any order.
  9. #Final generated number stored in "rand" variable.
  10. #218,340,105,584,896 possible combinations!
  11.  
  12. def digit_letter():
  13.  
  14. global dig_let
  15.  
  16. dig_let = randint(1,2)
  17.  
  18. if dig_let == 1:
  19. dig_let = digits,digits,digits,digits,digits
  20. else:
  21. dig_let = ascii_letters
  22.  
  23. shuffled = list("")
  24. x = list(range(4))
  25. for d in range(8):
  26. digit_letter()
  27. shuffled.append(choice(dig_let))
  28.  
  29. shuffle(shuffled)
  30. count = ""
  31.  
  32. rand = "".join(shuffled)
  33.  
  34. print (rand)
  35. input ()
  36.  
  37. randstr()

EDIT:

Woah, that just gives me a string of numbers.
This one should work:

  1. from random import choice, randint, shuffle
  2. from string import ascii_letters, digits
  3.  
  4. def randstr():
  5.  
  6. #Generates a random string.
  7. #String consists of numbers 0-9, letters a-z & A-Z.
  8. #Letters and number can be in any order.
  9. #Final generated number stored in "rand" variable.
  10. #218,340,105,584,896 possible combinations!
  11.  
  12. def digit_letter():
  13.  
  14. global dig_let
  15.  
  16. dig_let = randint(1,2)
  17.  
  18. if dig_let == 1:
  19. str_dig = str(digits)
  20. dig_combo = str_dig + str_dig + str_dig + str_dig + str_dig
  21. dig_let = choice(dig_combo)
  22. else:
  23. dig_let = ascii_letters
  24.  
  25. shuffled = list("")
  26. x = list(range(4))
  27. for d in range(8):
  28. digit_letter()
  29. shuffled.append(choice(dig_let))
  30.  
  31. shuffle(shuffled)
  32. count = ""
  33.  
  34. rand = "".join(shuffled)
  35.  
  36. print (rand)
  37. input ()
  38.  
  39. randstr()

Yeah, now the program appears to be choosing a more variety of numbers where as before it picked various numbers multiple times in a row.
Last edited by AutoPython; Sep 8th, 2009 at 7:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 233
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: My python program/function!

 
0
  #13
Sep 8th, 2009
You could define
  1. alnum = ascii_letter + (digits * 5)
at the top of your file, the remove the function digit_letter, and write
  1. for d in range(8):
  2. shuffled.append(choice(alnum))
also, you don't need x=list(range(4)) .
Last edited by Gribouillis; Sep 8th, 2009 at 7:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 14
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Re: My python program/function!

 
0
  #14
Sep 8th, 2009
I'm was already one step ahead of you. But it took a while. Here's what I have now.

  1. from random import choice, randint, shuffle
  2. from string import ascii_letters, digits
  3.  
  4. #Generates a random string.
  5. #String consists of numbers 0-9, letters a-z & A-Z.
  6. #Letters and number can be in any order.
  7. #Final generated number stored in "rand" variable.
  8. #218,340,105,584,896 possible combinations!
  9.  
  10. def randstr():
  11.  
  12. dig_let = str(digits)
  13. dig_let = (dig_let * 5) + ascii_letters
  14.  
  15. shuffled = list("")
  16. x = list(range(4))
  17. for d in range(8):
  18. shuffled.append(choice(dig_let))
  19.  
  20. shuffle(shuffled)
  21.  
  22. rand = "".join(shuffled)
  23.  
  24. print (rand)
  25. input ()
  26.  
  27. randstr()
Last edited by AutoPython; Sep 8th, 2009 at 8:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 233
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: My python program/function!

 
0
  #15
Sep 8th, 2009
"digits" is already a string, it doesn't need to be converted. Now we can replace the middle block with
  1. shuffled = list(choice(dig_let) for d in range(8))
Also, do we need to shuffle shuffled ?
Last edited by Gribouillis; Sep 8th, 2009 at 8:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 14
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Re: My python program/function!

 
0
  #16
Sep 8th, 2009
I just became super efficient .
FROM:

  1. from random import *
  2.  
  3. def randstr():
  4.  
  5. #Generates a random string.
  6. #String consists of numbers 1-9, letters a-z & A-Z.
  7. #Letters and number can be in any order.
  8. #Final generated number stored in "rand" variable.
  9. #767,544,201,216 possible combinations
  10.  
  11. lower = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")
  12. upper = ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
  13.  
  14. ch = randint(1,2)
  15. if ch == 1:
  16. case1 = lower
  17. else:
  18. case1 = upper
  19.  
  20. del ch
  21.  
  22. ch = randint(1,2)
  23. if ch == 1:
  24. case2 = lower
  25. else:
  26. case2 = upper
  27.  
  28. del ch
  29.  
  30. ch = randint(1,2)
  31. if ch == 1:
  32. case3 = lower
  33. else:
  34. case3 = upper
  35.  
  36. del ch
  37.  
  38. ch = randint(1,2)
  39. if ch == 1:
  40. case4 = lower
  41. else:
  42. case4 = upper
  43.  
  44. del ch
  45.  
  46. test1x = choice(case1)
  47. test2x = choice(case2)
  48. test3x = choice(case3)
  49. test4x = choice(case4)
  50. test1y = randint(1,9)
  51. test2y = randint(1,9)
  52. test3y = randint(1,9)
  53. test4y = randint(1,9)
  54. strconv1 = str(test1y)
  55. strconv2 = str(test2y)
  56. strconv3 = str(test3y)
  57. strconv4 = str(test4y)
  58. shuffled = [test1x, strconv1, test2x, strconv2, test3x, strconv3, test4x, strconv4]
  59. shuffle(shuffled)
  60. count = 0
  61. for number in shuffled:
  62.  
  63. if count == 0:
  64. sub_set = number
  65. count = count+1
  66.  
  67. if count == 8:
  68. rand = sub_set
  69. quit
  70.  
  71. if count != 0:
  72. if count != 8:
  73. sub_set = sub_set+number
  74. count = count+1
  75.  
  76. print (rand)
  77. del test1x
  78. del test2x
  79. del test3x
  80. del test4x
  81. del test1y
  82. del test2y
  83. del test3y
  84. del test4y
  85. del strconv1
  86. del strconv2
  87. del strconv3
  88. del strconv4
  89. del shuffled
  90. del count
  91. del rand
  92. del sub_set
  93. del number
  94. input ()
  95.  
  96. randstr()



TO:




  1. from random import choice, randint, shuffle
  2. from string import ascii_letters, digits
  3.  
  4. #Generates a random string.
  5. #String consists of numbers 0-9, letters a-z & A-Z.
  6. #Letters and number can be in any order.
  7. #Final generated number stored in "rand" variable.
  8. #218,340,105,584,896 possible combinations!
  9.  
  10. def randstr():
  11.  
  12. dig_let = (digits * 5) + ascii_letters
  13.  
  14. shuffled = list(choice(dig_let) for d in range (8))
  15.  
  16. shuffle(shuffled)
  17.  
  18. rand = "".join(shuffled)
  19.  
  20. print (rand)
  21. input ()
  22.  
  23. randstr()


THE POWER OF DANIWEB!
Last edited by AutoPython; Sep 8th, 2009 at 8:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 233
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: My python program/function!

 
0
  #17
Sep 8th, 2009
There is still place for improvement. Here is a new version
  1. from random import choice
  2. from string import ascii_letters, digits
  3. dig_let = (digits * 5) + ascii_letters
  4.  
  5. #Generates a random string.
  6. #String consists of numbers 0-9, letters a-z & A-Z.
  7. #Letters and number can be in any order.
  8. #Final generated number stored in "rand" variable.
  9. #218,340,105,584,896 possible combinations!
  10.  
  11. def randstr(size):
  12. return "".join(choice(dig_let) for d in range (size))
  13.  
  14. print(randstr(8))
  15. input()
Last edited by Gribouillis; Sep 8th, 2009 at 8:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 14
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Re: My python program/function!

 
0
  #18
Sep 8th, 2009
Wow, and I thought it couldn't get any better. But in this case would you have to use the variable size, or could use another variable like x or something.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 14
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Re: My python program/function!

 
0
  #19
Sep 8th, 2009
Okay, now, seriously, you can't possibly make it shorter.
Reply With Quote Quick reply to this message  
Reply

Tags
function, generator, python, random

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1610 | Replies: 18
Thread Tools Search this Thread



Tag cloud for function, generator, python, random
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC