egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

random string

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
simo
Owner


Joined: 22 Mar 2015
Posts: 941

PostPosted: Sun Jun 14, 2020 8:11 am    Post subject: random string Reply with quote

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
View user's profile Send private message
simo
Owner


Joined: 22 Mar 2015
Posts: 941

PostPosted: Sun Jun 14, 2020 3:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
CrazyCat
Revered One


Joined: 13 Jan 2002
Posts: 1032
Location: France

PostPosted: Sun Jun 14, 2020 6:46 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
simo
Owner


Joined: 22 Mar 2015
Posts: 941

PostPosted: Mon Jun 15, 2020 8:56 am    Post subject: Reply with quote

excellent tnx CrazyCat that works exactly as intented
Back to top
View user's profile Send private message
CrazyCat
Revered One


Joined: 13 Jan 2002
Posts: 1032
Location: France

PostPosted: Mon Jun 15, 2020 9:53 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
simo
Owner


Joined: 22 Mar 2015
Posts: 941

PostPosted: Mon Jun 15, 2020 10:25 am    Post subject: Reply with quote

oh thats even better tnx CrazyCat
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber