Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Random? (https://forums.graalonline.com/forums/showthread.php?t=134262281)

Astram 03-02-2011 04:56 AM

Random?
 
How would I make a random integer?
Like for chance... It needs to say a RANDOM number.
PHP Code:

function onActionGrab()
{
this.chat = ??? -Random Stuff Here- ???
} 


Jiroxys7 03-02-2011 05:14 AM

random(lownumber,highnumber);
combined with int(val);

To get a random int between 0 and 5, for example:
int(random(0,5.9999));

I suggest putting the 9's there for any of the integer-related random numbers, otherwise the chances of the high number showing up are slim to none.

DustyPorViva 03-02-2011 05:15 AM

this.chat = int(random(0,100)+.5);

Astram 03-02-2011 05:19 AM

Quote:

Originally Posted by DustyPorViva (Post 1634019)
this.chat = int(random(0,100)+.5);

whats the +.5 for? :confused:

Cloven 03-02-2011 05:36 AM

Quote:

Originally Posted by Astram (Post 1634021)
whats the +.5 for? :confused:

Because Graal's random functionality dislikes the high-end of a defined range for some reason. All values below the maximum value tend to have a higher probability of occurring. :(

Jiroxys7 03-02-2011 08:00 PM

Quote:

Originally Posted by DustyPorViva (Post 1634019)
this.chat = int(random(0,100)+.5);


That does look cleaner, but I suggest getting as close to +1 as reasonably possible. with only +0.5, there's a ~0.5 out of 5 chance that it will actually roll he high number. Which is why you should, at the very least, tag +0.99 at the end. add more 9's the more important it is.

DustyPorViva 03-02-2011 08:01 PM

Quote:

Originally Posted by Jiroxys7 (Post 1634119)
That does look cleaner, but I suggest getting as close to +1 as reasonably possible. with only +0.5, there's a ~0.5 out of 5 chance that it will actually roll he high number. Which is why you should, at the very least, tag +0.99 at the end. add more 9's the more important it is.

You are reading it wrong. It generates a random number then adds .5 to it. With int() this effectively rounds the number up, eliminating the problem.

Jiroxys7 03-02-2011 08:05 PM

Quote:

Originally Posted by DustyPorViva (Post 1634121)
You are reading it wrong. It generates a random number then adds .5 to it. With int() this effectively rounds the number up, eliminating the problem.

Ah, I see!


All times are GMT +2. The time now is 06:05 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.