Random Number generator
Posted by kinshuk_mishra (kinshuk_mishra), 17 March 2004Hi,
I want to know how to generate random numbers in TCL if some range is given. I can do that in K shell but I am unable to do that in TCL.
If i give two numbers say 100 and 100000 then between this the script should generate random numbers and the numbers generated should not be repeated. I am unable to do this.
Thanks in advance
Posted by Custard (Custard), 17 March 2004A quick glance at Dr Google returns:
Hope this helps.
(ps. it's not my code, just the first result..)
Posted by admin (Graham Ellis), 17 March 2004The rand function generates a random number between 0 and 1. Multiply the value it returns by the range you require, and add the offset you need, and convert to an integer. Thus
set value [expr floor( rand() * 999900) + 100]
You can run this again and you'll get a second number but there is a 1 in 999900 chance it will be the same number - you say you don't want this, so you should probably code a loop to keep asking for another random number until you meet your criterion.
Edit - hey, Custard, thanks for your answer. I started writing mine at lunchtime and got dragged away to do some training - just came back a moment ago and finished my doodlings - didn't meet to step on your toes
Posted by Custard (Custard), 17 March 2004It's fine Graham. (glad to hear your keeping busy)
To be honest it's been a while since I last TCLed. I had a feeling there was a rand proc. but the link above was the first relevant google for 'random and tcl'
Your solution is much more elegant.
edit your own (not yet published) comments
Other Forum Posts
Follow us on ...
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho