| | |
My python program/function!
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
Python Syntax (Toggle Plain Text)
import random, string print(''.join([random.choice(string.ascii_letters+"123456789") for f in range(8)]))
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:
EDIT:
Woah, that just gives me a string of numbers.
This one should work:
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.
@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:
Python Syntax (Toggle Plain Text)
from random import choice, randint, shuffle from string import ascii_letters, digits def randstr(): #Generates a random string. #String consists of numbers 0-9, letters a-z & A-Z. #Letters and number can be in any order. #Final generated number stored in "rand" variable. #218,340,105,584,896 possible combinations! def digit_letter(): global dig_let dig_let = randint(1,2) if dig_let == 1: dig_let = digits,digits,digits,digits,digits else: dig_let = ascii_letters shuffled = list("") x = list(range(4)) for d in range(8): digit_letter() shuffled.append(choice(dig_let)) shuffle(shuffled) count = "" rand = "".join(shuffled) print (rand) input () randstr()
EDIT:
Woah, that just gives me a string of numbers.
This one should work:
Python Syntax (Toggle Plain Text)
from random import choice, randint, shuffle from string import ascii_letters, digits def randstr(): #Generates a random string. #String consists of numbers 0-9, letters a-z & A-Z. #Letters and number can be in any order. #Final generated number stored in "rand" variable. #218,340,105,584,896 possible combinations! def digit_letter(): global dig_let dig_let = randint(1,2) if dig_let == 1: str_dig = str(digits) dig_combo = str_dig + str_dig + str_dig + str_dig + str_dig dig_let = choice(dig_combo) else: dig_let = ascii_letters shuffled = list("") x = list(range(4)) for d in range(8): digit_letter() shuffled.append(choice(dig_let)) shuffle(shuffled) count = "" rand = "".join(shuffled) print (rand) input () 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.
You could define
at the top of your file, the remove the function digit_letter, and write
also, you don't need
python Syntax (Toggle Plain Text)
alnum = ascii_letter + (digits * 5)
python Syntax (Toggle Plain Text)
for d in range(8): shuffled.append(choice(alnum))
x=list(range(4)) . Last edited by Gribouillis; Sep 8th, 2009 at 7:53 pm.
I'm was already one step ahead of you. But it took a while. Here's what I have now.
Python Syntax (Toggle Plain Text)
from random import choice, randint, shuffle from string import ascii_letters, digits #Generates a random string. #String consists of numbers 0-9, letters a-z & A-Z. #Letters and number can be in any order. #Final generated number stored in "rand" variable. #218,340,105,584,896 possible combinations! def randstr(): dig_let = str(digits) dig_let = (dig_let * 5) + ascii_letters shuffled = list("") x = list(range(4)) for d in range(8): shuffled.append(choice(dig_let)) shuffle(shuffled) rand = "".join(shuffled) print (rand) input () randstr()
Last edited by AutoPython; Sep 8th, 2009 at 8:13 pm.
"digits" is already a string, it doesn't need to be converted. Now we can replace the middle block with
Also, do we need to shuffle shuffled ?
python Syntax (Toggle Plain Text)
shuffled = list(choice(dig_let) for d in range(8))
Last edited by Gribouillis; Sep 8th, 2009 at 8:26 pm.
I just became super efficient
.
FROM:
TO:
THE POWER OF DANIWEB!
. FROM:
Python Syntax (Toggle Plain Text)
from random import * def randstr(): #Generates a random string. #String consists of numbers 1-9, letters a-z & A-Z. #Letters and number can be in any order. #Final generated number stored in "rand" variable. #767,544,201,216 possible combinations 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") 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") ch = randint(1,2) if ch == 1: case1 = lower else: case1 = upper del ch ch = randint(1,2) if ch == 1: case2 = lower else: case2 = upper del ch ch = randint(1,2) if ch == 1: case3 = lower else: case3 = upper del ch ch = randint(1,2) if ch == 1: case4 = lower else: case4 = upper del ch test1x = choice(case1) test2x = choice(case2) test3x = choice(case3) test4x = choice(case4) test1y = randint(1,9) test2y = randint(1,9) test3y = randint(1,9) test4y = randint(1,9) strconv1 = str(test1y) strconv2 = str(test2y) strconv3 = str(test3y) strconv4 = str(test4y) shuffled = [test1x, strconv1, test2x, strconv2, test3x, strconv3, test4x, strconv4] shuffle(shuffled) count = 0 for number in shuffled: if count == 0: sub_set = number count = count+1 if count == 8: rand = sub_set quit if count != 0: if count != 8: sub_set = sub_set+number count = count+1 print (rand) del test1x del test2x del test3x del test4x del test1y del test2y del test3y del test4y del strconv1 del strconv2 del strconv3 del strconv4 del shuffled del count del rand del sub_set del number input () randstr()
TO:
Python Syntax (Toggle Plain Text)
from random import choice, randint, shuffle from string import ascii_letters, digits #Generates a random string. #String consists of numbers 0-9, letters a-z & A-Z. #Letters and number can be in any order. #Final generated number stored in "rand" variable. #218,340,105,584,896 possible combinations! def randstr(): dig_let = (digits * 5) + ascii_letters shuffled = list(choice(dig_let) for d in range (8)) shuffle(shuffled) rand = "".join(shuffled) print (rand) input () randstr()
THE POWER OF DANIWEB!
Last edited by AutoPython; Sep 8th, 2009 at 8:34 pm.
There is still place for improvement. Here is a new version
python Syntax (Toggle Plain Text)
from random import choice from string import ascii_letters, digits dig_let = (digits * 5) + ascii_letters #Generates a random string. #String consists of numbers 0-9, letters a-z & A-Z. #Letters and number can be in any order. #Final generated number stored in "rand" variable. #218,340,105,584,896 possible combinations! def randstr(size): return "".join(choice(dig_let) for d in range (size)) print(randstr(8)) input()
Last edited by Gribouillis; Sep 8th, 2009 at 8:45 pm.
![]() |
Similar Threads
- How to get PDF file as output from the python program to give a print (Python)
- how to end program/function (C++)
- Word Jumble python program (Python)
- Python program using a funtion to solve the fibonacci sequence (Python)
- Enable/Disable port through Python program (Python)
- Urgent : Question on editing a python program .... (Python)
- Python Sleep Function: (Python)
- running a process from a python program (Python)
Other Threads in the Python Forum
- Previous Thread: Problem calling 'super' inheriting PyQt4 objects
- Next Thread: Return Statement Dilemma
Views: 1610 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for function, generator, python, random
address advice aliased apax array avogadro beginner c++ calculator class code compression convert copy corners cturtle decimals decode development dictionary editing embed enter examples excel file filter ftp function generator gui image incode input ip itunes java leftmouse linux list lists loan loop math matrix media microphone microsoft module mouse mysql newb number numbers obexftp opensource parameter php programming projects py2exe pygame pygtk pyopengl python random recursion recursive redirect ruby rubyconf script server silverlight skinning slicenotation sprite sql sqlite ssh string sum syntax table text thread threading tkinter tlapse tutorial ubuntu update url urllib urllib2 variable verify void voip wxpython






