Welcome, guest | Sign In | My Account | Store | Cart

this is a handy peice of code when you dont wanna let users chose they own passwords for a sensitive system.... Or even if you want a more secure passpword combo yourself... It handles letters, numbers, upper and lower case characters.... be kind, its my first piece of code.

Tcl, 31 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
puts stdout "How many character long?"
set length [expr [gets stdin]+1]
puts stdout "Upper case characters too? y/n"
set upper [string toupper [gets stdin]]
set loop 1
set pass "generated pass="

proc random {range} {
    return [expr {round(rand()*$range)}]
}
# generate random number within given range

while {$loop != $length} {
	incr loop
	if {[random 4] == 1} { 
		append pass [format "%c" [expr [random 9]+48]] 
	} else { 
		set letter [format "%c" [expr [random 25]+97]] 
		if {$upper == "Y" && [random 1] } { 
			set letter [string toupper $letter]
		}	
		append pass $letter 
	}
}
# while there are still numbers to be appended.
# 1/4 chance a number is added to the string.
# 3/4 chance a letter is added to the string.
# 1/2 chance the letter is uppercased,
# provided the user has selected Y to uppercase

puts stdout "$pass"

this is my first peice of tcl, and I would apreciate bugs sent to me at davou000@yahoo.ca

This code is gnu'ed.... please see google for a copy of the gnu public user licence.

1 comment

Jatin Sanghvi 18 years, 3 months ago  # | flag

If one invokes "random 2", there will be 50% probability that "1" will be returned while 25% probability for "0" and "2" each. Hence outputs of "random" are not equiprobable.

I think it'll be better if floor() function is used in place of round() in the "random" procedure and "range" is incremented by 1 in each call to "random" in the code.

Created by david windsor on Mon, 6 Dec 2004 (MIT)
Tcl recipes (162)
david windsor's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks