| View previous topic :: View next topic |
| Author |
Message |
simo Owner
Joined: 22 Mar 2015 Posts: 941
|
Posted: Sun Jun 14, 2020 8:11 am Post subject: random string |
|
|
| Code: |
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:
| Quote: |
set newPassX [randomRangeString 8]
:$::network-$newPassX.$newPassX.$newPassX.IP
|
|
|
| Back to top |
|
 |
simo Owner
Joined: 22 Mar 2015 Posts: 941
|
Posted: Sun Jun 14, 2020 3:05 pm Post subject: |
|
|
basically was wondering if this proc could be changed to output 4 different strings of 8
like this:
$::network.randomstingof8.randomstingof8.randomstingof8.IP |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Sun Jun 14, 2020 6:46 pm Post subject: |
|
|
Add an argument to the proc:
| Code: | # 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
} |
_________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
simo Owner
Joined: 22 Mar 2015 Posts: 941
|
Posted: Mon Jun 15, 2020 8:56 am Post subject: |
|
|
| excellent tnx CrazyCat that works exactly as intented |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Mon Jun 15, 2020 9:53 am Post subject: |
|
|
Just a short modification:
| Code: | | 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 _________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
simo Owner
Joined: 22 Mar 2015 Posts: 941
|
Posted: Mon Jun 15, 2020 10:25 am Post subject: |
|
|
| oh thats even better tnx CrazyCat |
|
| Back to top |
|
 |
|