Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-19-2012, 05:42 PM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
Random number draw

PHP Code:
//#CLIENTSIDE
function onCreated() {
 
this.number //5 or 10

How do I make the client randomly pick from two numbers. In this case, I need it to pick either 5 or 10, but be random.
Reply With Quote
  #2  
Old 12-19-2012, 05:48 PM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
PHP Code:
//#CLIENTSIDE 
function onCreated() { 
 
this.number int(random(1,2.9))*5;//5 or 10

I used 2.9 because it generates a decimal number, so the occurance of 2 would be less if it was just 2.0
Reply With Quote
  #3  
Old 12-19-2012, 06:01 PM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
Quote:
PHP Code:
//#CLIENTSIDE
function onCreated() {
this.number = int(random(1,2.9))*5;//5 or 10
}
I used 2.9 because it generates a decimal number, so the occurance of 2 would be less if it was just 2.0
So if I were to be doing 1 or 3, itd be..

PHP Code:
this.number int(random(1,3)); 
Just tried what I wrote above..It draws 2.
I'm not too familiar with random integer scripts but of what I know that's not right because the only number between 1 and 3 is 2.
I need it to only pick from 1 or 3.

Last edited by greggiles; 12-19-2012 at 06:18 PM..
Reply With Quote
  #4  
Old 12-19-2012, 06:20 PM
Starfire2001 Starfire2001 is offline
Unholy Nation
Starfire2001's Avatar
Join Date: Dec 2010
Location: The streets.
Posts: 156
Starfire2001 will become famous soon enough
Quote:
Originally Posted by greggiles View Post
So if I were to be doing 1 or 3, itd be..

PHP Code:
this.number int(random(1,3); 
No.

random(1,3) gives you a random number between 1 and 3 (though never the high number, in this case 3.) So for example, random(1,3) might give you 2.5, or 1.72, etc.

int() then rounds the number down to the nearest integer. (so 2.5 becomes 2, 1.99 becomes 1.)

So

PHP Code:
this.number int(random(1,3)); 
Would randomly select either 1 or 2.
__________________
-Ph8
Reply With Quote
  #5  
Old 12-19-2012, 06:52 PM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
Quote:
Originally Posted by Starfire2001 View Post
No.

random(1,3) gives you a random number between 1 and 3 (though never the high number, in this case 3.) So for example, random(1,3) might give you 2.5, or 1.72, etc.

int() then rounds the number down to the nearest integer. (so 2.5 becomes 2, 1.99 becomes 1.)

So

PHP Code:
this.number int(random(1,3)); 
Would randomly select either 1 or 2.
Ah I didn't remember if it did show the higher number. I was being safe.. also he said 1 or 3.. not 1 to 3.. ? so

PHP Code:
this.number int(random(0,2))*2
Would do 1.. plus it resulted in 1.. it added 1*2.. so it was either 1 or 3.
Reply With Quote
  #6  
Old 12-19-2012, 07:17 PM
Starfire2001 Starfire2001 is offline
Unholy Nation
Starfire2001's Avatar
Join Date: Dec 2010
Location: The streets.
Posts: 156
Starfire2001 will become famous soon enough
Quote:
Originally Posted by scriptless View Post
I was being safe.. also he said 1 or 3.. not 1 to 3.. ?
Indeed, was just explaining what random() and int() were actually doing.
__________________
-Ph8
Reply With Quote
  #7  
Old 12-19-2012, 07:17 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
heres my input :P

PHP Code:
//all numbers which can be chosen; also strings possible
temp.number_possible = {358104};

//check ph8's post for the int() and random() description
temp.number_display temp.number_possibleint(random(0temp.number_possible.size())) ];

player.chat temp.number_display
__________________
MEEP!
Reply With Quote
  #8  
Old 12-20-2012, 01:48 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Those are some of the most weirdly complicated ways of doing this I've seen.

PHP Code:
function pickRandomNumber() {
  if (
random(01) < 0.5) {
    return 
5;
  } else {
    return 
10;
  }

Usually the simplest solution is best. Code readability > tricky math. It's a lot easier to look at mine and see what it does than it is to stop and mentally parse out int(random(1,2.9))*5.
__________________
Reply With Quote
  #9  
Old 12-20-2012, 02:45 AM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
PHP Code:
temp.number = (random(01) < 0.5 5:10); 

Edit:
For more information on the ternary operator.
http://en.wikipedia.org/wiki/Ternary_operator
__________________
http://i.imgur.com/OOJbW.jpg

Last edited by BlueMelon; 12-20-2012 at 04:34 AM..
Reply With Quote
  #10  
Old 12-20-2012, 03:21 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Quote:
Originally Posted by BlueMelon View Post
PHP Code:
temp.number = (random(01) < 0.5 5:10); 
I was going to post about ternary, but Chris obviously knows what it is and I think such a thing is lost on the level that Greggiles is scripting at. But if you're going to post it in the attempt of helping then you should explain what it is and why it's useful.
Reply With Quote
  #11  
Old 12-20-2012, 05:15 AM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
Quote:
Originally Posted by cbk1994 View Post
Those are some of the most weirdly complicated ways of doing this I've seen.

PHP Code:
function pickRandomNumber() {
  if (
random(01) < 0.5) {
    return 
5;
  } else {
    return 
10;
  }

Usually the simplest solution is best. Code readability > tricky math. It's a lot easier to look at mine and see what it does than it is to stop and mentally parse out int(random(1,2.9))*5.
Bah, this is elementry level math. Nothing tricky about it.. Had it been more complicated, yes I would have done like chris showed however I prefer doing it as BlueMelon showed at that point. It was just kinda the first thing that poped into my head to help, without adding a bunch of lines to his script.
Reply With Quote
  #12  
Old 12-20-2012, 03:53 PM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
@scriptless,

Its all about readability and efficiency.
__________________
http://i.imgur.com/OOJbW.jpg
Reply With Quote
  #13  
Old 12-20-2012, 03:59 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by BlueMelon View Post
@scriptless,

Its all about readability and efficiency.
And consistency. With a random float between 1 and 2.9, you have a higher chance to get a 1 out of it. That's not what you want.
Reply With Quote
  #14  
Old 12-20-2012, 04:34 PM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
Quote:
I was going to post about ternary, but Chris obviously knows what it is and I think such a thing is lost on the level that Greggiles is scripting at. But if you're going to post it in the attempt of helping then you should explain what it is and why it's useful.

I figured it out! And sorry, I'm really trying to learn.
Reply With Quote
  #15  
Old 12-20-2012, 04:48 PM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
Quote:
Originally Posted by BlueMelon View Post
@scriptless,

Its all about readability and efficiency.
Then cbk's is more easily readable. However, I still prefer doing it the way you posted.
Reply With Quote
  #16  
Old 12-20-2012, 04:53 PM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
Quote:
Originally Posted by scriptless View Post
Then cbk's is more easily readable. However, I still prefer doing it the way you posted.
I wouldn't agree, the ternary operator is easy to read if you use it properly.

@Crow,
Yes consistency is also key.
__________________
http://i.imgur.com/OOJbW.jpg
Reply With Quote
  #17  
Old 12-20-2012, 05:49 PM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
Quote:
Originally Posted by BlueMelon View Post
I wouldn't agree, the ternary operator is easy to read if you use it properly.

@Crow,
Yes consistency is also key.
Assumming someone who know's nothing about scripting, they probably have a better chance of understanding cbk's before yours.. I didn't even know about the ternary operator untill about 2 years ago, or 3.. I forget.. and I have been scripting for 10 years now..
Reply With Quote
  #18  
Old 12-20-2012, 06:10 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by scriptless View Post
Assumming someone who know's nothing about scripting, they probably have a better chance of understanding cbk's before yours.. I didn't even know about the ternary operator untill about 2 years ago, or 3.. I forget.. and I have been scripting for 10 years now..
Your snippet doesn't make any logical sense, though, so it's not any better. I hope you realize that.
Reply With Quote
  #19  
Old 12-20-2012, 06:17 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Crow View Post
Your snippet doesn't make any logical sense, though, so it's not any better. I hope you realize that.
This.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #20  
Old 12-20-2012, 11:14 PM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
Quote:
Originally Posted by scriptless View Post
Assumming someone who know's nothing about scripting, they probably have a better chance of understanding cbk's before yours.. I didn't even know about the ternary operator untill about 2 years ago, or 3.. I forget.. and I have been scripting for 10 years now..
Are you serious? Do you know any other programming/scripting languages? The ternary operator is taught with all the basics of any language... -.-'
__________________
http://i.imgur.com/OOJbW.jpg
Reply With Quote
  #21  
Old 12-21-2012, 12:02 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Quote:
Originally Posted by BlueMelon View Post
Are you serious? Do you know any other programming/scripting languages? The ternary operator is taught with all the basics of any language... -.-'
This is a thread attempting to help someone with something like random(). Ternary is just going to confuse them at this point.
Reply With Quote
  #22  
Old 12-22-2012, 10:12 PM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
Quote:
Originally Posted by BlueMelon View Post
Are you serious? Do you know any other programming/scripting languages? The ternary operator is taught with all the basics of any language... -.-'
I never went to college to learn any languages. I started when I needed to learn a language to use to further my work with Reverse Engeneering Graal around the 2.x days. (Ending at 2.310). But I do however, know a little bit (enough to kinda follow along and figure stuff out) in Delphi/Pascal, C, C++, C#, and various web languages.

It would most likely confuse any new person who has yet to begin a class tho is what I was meaning. Someone that knows nothing yet would be more likely to read cbk's.

Quote:
Originally Posted by Crow View Post
Your snippet doesn't make any logical sense, though, so it's not any better. I hope you realize that.
True, tho it does work.
Reply With Quote
  #23  
Old 12-23-2012, 03:53 AM
BboyEatsbacon BboyEatsbacon is offline
The Bacon Man
BboyEatsbacon's Avatar
Join Date: Feb 2011
Location: United States
Posts: 60
BboyEatsbacon will become famous soon enough
You all are making it so complex, but with GS2, you can tackle this problem simply.

PHP Code:
function onCreated() {
  
this.possibilities = { "5""10" };

  
drawRandomNumber();
}

function 
drawRandomNumber() {
  
this.number randomstring(this.possibilities);

While this is not good practice for other engines, GS2 supports inputting a string as an integer, so this script would work smoothly.
Reply With Quote
  #24  
Old 12-23-2012, 01:34 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by BboyEatsbacon View Post
You all are making it so complex, but with GS2, you can tackle this problem simply.

PHP Code:
function onCreated() {
  
this.possibilities = { "5""10" };

  
drawRandomNumber();
}

function 
drawRandomNumber() {
  
this.number randomstring(this.possibilities);

While this is not good practice for other engines, GS2 supports inputting a string as an integer, so this script would work smoothly.
randomString() is not a good option. It's been bugged for a long time, and sometimes results in a blank result. With sometimes, I really mean fairly often.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #25  
Old 12-23-2012, 04:43 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by xXziroXx View Post
randomString() is not a good option. It's been bugged for a long time, and sometimes results in a blank result. With sometimes, I really mean fairly often.
Stefan said he would fix it in Nov. 2011:

Quote:
Originally Posted by Stefan View Post
Ok will fix that behaviour of randomstring(), it was originally not made for getting arrays as input.
Not sure if it was ever fixed or not though.
__________________

Last edited by cbk1994; 12-23-2012 at 07:34 PM..
Reply With Quote
  #26  
Old 12-23-2012, 06:52 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by cbk1994 View Post
Stefan said he would fix it on in Nov. 2011:



Not sure if it was ever fixed or not though.
It's not been fixed, I tried it a few days ago.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #27  
Old 12-24-2012, 05:22 AM
BboyEatsbacon BboyEatsbacon is offline
The Bacon Man
BboyEatsbacon's Avatar
Join Date: Feb 2011
Location: United States
Posts: 60
BboyEatsbacon will become famous soon enough
Quote:
Originally Posted by xXziroXx View Post
randomString() is not a good option. It's been bugged for a long time, and sometimes results in a blank result. With sometimes, I really mean fairly often.
Ah okay.

It's always worked for me, my apologies.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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