Tcl language Note: Tcl practice

Source: Internet
Author: User
Tags rand uppercase letter

First, the practice of random numbers

1. Randomly generate an integer between the maximum and the minimum value

proc Random {min max} {      return [expr round (($max-$min) *rand () +$min)]  }

2. Generate a random integer that is not duplicated between Num min to max

Proc randomunique {min Max num} {
Array set arr {}

#如果要求生成的数量超过max和min相减的个数 +1
If {$num >[expr $max-$min +1]} {
Return ""
}

#当生成的随机数量没有达到要求的数量, call a function that generates a random integer
While {[Array size arr]< $num} {
Set x [Random $min $max]
Set arr ($x) ""
}
return [array names arr]
}

3. Random lowercase letters

proc Randlow {} {    return122]}

4. Random Uppercase letters

proc Randhigh {} {    return)]}

5. Randomly generate a list of content

proc Randlist {lst} {    set0 [expr [llength $lst]-1]    ]return  [lindex $lst $id]}

6. Generate a random lowercase or uppercase letter

proc RANDLH {} {    return  [rand[randlist {low-high}]}

7. Generate a random uppercase, lowercase letter, or number

proc Randlhn {} {    return  [rand[randlist {Low High Num}]}

8. Generate a lowercase, uppercase, numeric string with a length of random value between Minlen and MaxLen

proc Randstrlhn {Minlen maxlen} {    set  len [random $minLen $maxLen]    Set  ""for     {set0} {$i <$len} {incr i} {        append str [ RANDLHN]    }    return  $str}

Tcl language Note: Tcl practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.