Graal Forums

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

greggiles 12-19-2012 05:42 PM

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.

scriptless 12-19-2012 05:48 PM

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

greggiles 12-19-2012 06:01 PM

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.

Starfire2001 12-19-2012 06:20 PM

Quote:

Originally Posted by greggiles (Post 1709945)
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.

scriptless 12-19-2012 06:52 PM

Quote:

Originally Posted by Starfire2001 (Post 1709947)
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.

Starfire2001 12-19-2012 07:17 PM

Quote:

Originally Posted by scriptless (Post 1709951)
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.

callimuc 12-19-2012 07:17 PM

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


cbk1994 12-20-2012 01:48 AM

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.

BlueMelon 12-20-2012 02:45 AM

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

DustyPorViva 12-20-2012 03:21 AM

Quote:

Originally Posted by BlueMelon (Post 1709995)
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.

scriptless 12-20-2012 05:15 AM

Quote:

Originally Posted by cbk1994 (Post 1709990)
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.

BlueMelon 12-20-2012 03:53 PM

@scriptless,

Its all about readability and efficiency.

Crow 12-20-2012 03:59 PM

Quote:

Originally Posted by BlueMelon (Post 1710035)
@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.

greggiles 12-20-2012 04:34 PM

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.

scriptless 12-20-2012 04:48 PM

Quote:

Originally Posted by BlueMelon (Post 1710035)
@scriptless,

Its all about readability and efficiency.

Then cbk's is more easily readable. However, I still prefer doing it the way you posted.


All times are GMT +2. The time now is 12:09 AM.

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