Graal Forums

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

Emera 10-26-2011 05:27 PM

Choosing Random Values
 
I am trying to make a system, just for testing purposes that picks out a random number or word from an array and echo it.
Example;
PHP Code:

//#CLIENTSIDE
function onCreated() {
  
//Define array
  
this.array = {"One""Two""Three""For"};
}

function 
onWeaponFired() {
  
//Do stuff here
  
echo(result);


I want it to randomly select either of the values in the array, and echo the result. Any ideas?

fowlplay4 10-26-2011 05:31 PM

If your array is guaranteed to have more than 1 element:

echo(randomstring(this.array));

will do exactly what you want to. You can also do:

temp.result = this.array[int(random(0, this.array.size()))];

Emera 10-26-2011 06:36 PM

Quote:

Originally Posted by fowlplay4 (Post 1671885)
If your array is guaranteed to have more than 1 element:

echo(randomstring(this.array));

will do exactly what you want to. You can also do:

temp.result = this.array[int(random(0, this.array.size()))];

Thank you very much :D

cbk1994 10-26-2011 09:59 PM

I would avoid randomstring entirely, it's only going to lead to problems in the future when somebody changes your script so that it's not guaranteed to have at least two elements.

Emera 10-26-2011 10:46 PM

So using;
NPC Code:
temp.result = this.array[int(random(0, this.array.size()))];


Is my best bet? Can you explain this to me in further detail please? What does the [int(random(0,this.array.size()))]; bit mean?

fowlplay4 10-26-2011 10:52 PM

int(value) - Converts the value to an integer (rounded). I.e: int(2.5) - returns 2
random(a, b) - Returns a random value between a and b. It'll never equal b though.
array[i] - The element in the array.

It basically generates a valid random index in the array.

PHP Code:

temp.ind int(random(0this.array.size()));
temp.result this.array[temp.ind]; 


Emera 10-26-2011 10:54 PM

Quote:

Originally Posted by fowlplay4 (Post 1671921)
int(value) - Converts the value to an integer (rounded). I.e: int(2.5) - returns 2
random(a, b) - Returns a random value between a and b. It'll never equal b though.
array[i] - The element in the array.

It basically generates a valid random index in the array.

PHP Code:

temp.ind int(random(0this.array.size()));
temp.result this.array[temp.ind]; 


Ah ok. Thank you :D

xXziroXx 10-27-2011 12:39 AM

Quote:

Originally Posted by cbk1994 (Post 1671915)
I would avoid randomstring entirely, it's only going to lead to problems in the future when somebody changes your script so that it's not guaranteed to have at least two elements.

Uh, what?

fowlplay4 10-27-2011 01:41 AM

Quote:

Originally Posted by xXziroXx (Post 1671930)
Uh, what?

Try running this a 10 or 20 times:

PHP Code:

function onCreated() {
  
temp.= {"a"};
  echo(
randomstring(temp.a));


Should echo "a" every time but it doesn't.

xXziroXx 10-27-2011 02:37 AM

Quote:

Originally Posted by fowlplay4 (Post 1671935)
Try running this a 10 or 20 times:

PHP Code:

function onCreated() {
  
temp.= {"a"};
  echo(
randomstring(temp.a));


Should echo "a" every time but it doesn't.

What the hell!? That's shady.

Admins 11-04-2011 07:42 PM

Ok will fix that behaviour of randomstring(), it was originally not made for getting arrays as input.

fowlplay4 11-04-2011 07:52 PM

Quote:

Originally Posted by Stefan (Post 1673119)
Ok will fix that behaviour of randomstring(), it was originally not made for getting arrays as input.

Can you add a random() to TGraalVar (or whatever the main class is) so we can retrieve random elements or characters from arrays and strings. I.e:

"abcdefg".random() - can return "a" or "b" or etc.
{1,2,3,4}.random() - can return 1, 2, or etc.


All times are GMT +2. The time now is 07:10 AM.

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