Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Random Number generator

Posted by kinshuk_mishra (kinshuk_mishra), 17 March 2004
Hi,

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.

For example.
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.

Please help

Thanks in advance

Kinshuk

Posted by Custard (Custard), 17 March 2004
A quick glance at Dr Google returns:

http://tcl.apache.org/sources/tcl-rivet/rivet/rivet-tcl/random.tcl.html


Hope this helps.

B

(ps. it's not my code, just the first result..)

Posted by admin (Graham Ellis), 17 March 2004
The 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 2004
It'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.

B



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

2 unpublished comments pending on this page

edit your own (not yet published) comments

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho