Created by [CecilWesterhof]. '''Functions to Get Random Integers on a *NIX System''' To get better random integers you can use /dev/random and /dev/urandom. They are mostly the same, the difference is that urandom will not block, but that could sacrifice the randomness. The first proc is a proc to get a a random number. It gives a number in the range [0, 2 ** 32 - 1] (default), or in the range [0, 2 ** 64 - 1] when longVersion is true. Default it uses /dev/urandom, but when the randomness is important (encryption, passwords, …) use secure with True and it will use /dev/random. ====== proc getRandomIntNix {{secure False} {longVersion False}} { if ${secure} { set randFile /dev/random } else { set randFile /dev/urandom } if ${longVersion} { set bytes 8 set format wu } else { set bytes 4 set format iu } set randDev [open ${randFile} rb] set random [read ${randDev} ${bytes}] close ${randDev} binary scan ${random} ${format} randomI return ${randomI} } ====== Often you need another range (for example [-5, 12]), for this I created the following proc. ====== proc getRandomIntInRangeNix {min max {secure False} {longVersion False}} { if {${max} < ${min}} { error "Error: [info level 0] min max ?secure ?longVersion??" } if ${longVersion} { set maxMod [expr 2 ** 64] } else { set maxMod [expr 2 ** 32] } set modulo [expr {${max} - ${min} + 1}] if {${modulo} > ${maxMod}} { error "Modulo should be <= ${maxMod} (${modulo})" } set random [getRandomIntNix ${secure} ${longVersion}] expr {${random} % ${modulo} + ${min}} } ====== ---- As always: comments, tips and questions are appreciated. <>Linux | Numerics | Utilities