This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

random string

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
s
simo
Revered One
Posts: 1073
Joined: Sun Mar 22, 2015 2:41 pm

random string

Post by simo »

Code: Select all

proc randomRangeString {length {chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"}} {
    set range [expr {[string length $chars]-1}]

    set txt ""
    for {set i 0} {$i < $length} {incr i} {
       set pos [expr {int(rand()*$range)}]
       append txt [string range $chars $pos $pos]
    }
    return $txt
}
when using this random string proc is there a way to output 3 different results in this because atm it seems to produce 3 same outputs:

set newPassX [randomRangeString 8]

:$::network-$newPassX.$newPassX.$newPassX.IP
s
simo
Revered One
Posts: 1073
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

basically was wondering if this proc could be changed to output 4 different strings of 8

like this:

$::network.randomstingof8.randomstingof8.randomstingof8.IP
User avatar
CrazyCat
Revered One
Posts: 1217
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

Add an argument to the proc:

Code: Select all

# usage : randomString <length> <iterations>
proc randomString { length iter } {
   set chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
   set range [string length $chars]
   set rlist {}
   for {set j 0 } {$j < $iter} {incr j} {
      set txt ""
      for {set i 0} {$i < $length} {incr i} {
         set pos [expr {int(rand()*$range)}]
         append txt [string index $chars $pos]
      }
      lappend rlist $txt
   }
   return $rlist
}
s
simo
Revered One
Posts: 1073
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

excellent tnx CrazyCat that works exactly as intented
User avatar
CrazyCat
Revered One
Posts: 1217
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

Just a short modification:

Code: Select all

proc randomString { {length 8} {iter 1} } {
This will allow to:
- simply call randomString => will return 1 random string of 8 chars
- call randomString X (X is integer)=> will return 1 random string of X chars
- call randomString X Y (X and Y are integer)=> will return Y random strings of X chars
s
simo
Revered One
Posts: 1073
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

oh thats even better tnx CrazyCat
Post Reply