This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

[SOLVED] expr / rand / int help

Help for those learning Tcl or writing their own scripts.
Post Reply
M
MIODude
Voice
Posts: 32
Joined: Mon Oct 09, 2006 6:26 pm

[SOLVED] expr / rand / int help

Post by MIODude »

Hi Again!

I keep trying variations of this.. but can't quite get it

Code: Select all

set maxpoint 6000
set variable [expr {int([rand $maxpoint] * .001)}]
I want the possible values to be 0-6 with 2 decimal places (ie.. 5.23, 4.75, etc)

the current way i have it, it spits out only whole numbers (I'm assuming because the int is outside of everything.. but if I try to put .001 outside of the {}, i get errors like "invalid bareword "int"", or "can't use non-numeric string as operand of "*"

i have the int in there so I can control the number of decimal places (else it comes out with things like 3.54338238)
Last edited by MIODude on Fri Feb 26, 2010 7:10 pm, edited 1 time in total.
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

Post by w00f »

format %.2f <value>
will output what you want.

ie:
.format %.2f "5.436987"
5.44
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

MIODude,
Since you want a floating point number returned, why do you convert it to an integer in the first place? Since rand will return an integer between 0 and limit-1, simply dividing it with an appropriate divisor (of type float) should render at most the number of decimals that you desire.

Code: Select all

set maxpoint 600
set variable [expr [rand $maxpoint] / 100.0]
I suppose I could illustrate the proper way you would use the int function in this case, though you'll find that it does actually nothing in this case, as the random value is already an integer...

Code: Select all

set maxpoint 6000
set variable [expr {int([rand $maxpoint]) * 0.001}]
This would, however, still return 3 decimals. To sort that, you'd first have to divide the random number by ten, then convert it into an integer, and then divide by one hundred. Also, to properly round up/down, you should really use round() instead of int(), which simply floors the value.

Code: Select all

set maxpoint 6000
set variable [expr {round([rand $maxpoint] / 10) / 100.0}]
Finally, be adviced that in such trivial cases like this, you really don't need the {}, as they're only there to prevent "double evaluation", using them won't change much in these simple cases - just remember that if you do use them, you'll have to keep the whole expression within them.
NML_375
M
MIODude
Voice
Posts: 32
Joined: Mon Oct 09, 2006 6:26 pm

Post by MIODude »

Thanks :)

I used this one
set variable [expr [rand $maxpoint] / 100.0]

i must have made a mistake when i tried without the int before.. as i was getting some pretty weird numbers


edit: and thanks for clarifying about the {}... i've always been confused on which brackets to use for which things..
Post Reply