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
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 09:40 AM.


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