egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[SOLVED] expr / rand / int help

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
MIODude
Voice


Joined: 09 Oct 2006
Posts: 32

PostPosted: Fri Feb 26, 2010 11:39 am    Post subject: [SOLVED] expr / rand / int help Reply with quote

Hi Again!

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

Code:

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
Back to top
View user's profile Send private message
w00f
Halfop


Joined: 04 Oct 2006
Posts: 49

PostPosted: Fri Feb 26, 2010 5:21 pm    Post subject: Reply with quote

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

ie:
.format %.2f "5.436987"
5.44
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Fri Feb 26, 2010 5:41 pm    Post subject: Reply with quote

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:
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:
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:
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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
MIODude
Voice


Joined: 09 Oct 2006
Posts: 32

PostPosted: Fri Feb 26, 2010 5:56 pm    Post subject: Reply with quote

Thanks Smile

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..
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber