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 10-26-2011, 05:27 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
Question 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?
__________________
Reply With Quote
  #2  
Old 10-26-2011, 05:31 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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()))];
__________________
Quote:
Reply With Quote
  #3  
Old 10-26-2011, 06:36 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
Quote:
Originally Posted by fowlplay4 View Post
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
__________________
Reply With Quote
  #4  
Old 10-26-2011, 09:59 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
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.
__________________
Reply With Quote
  #5  
Old 10-26-2011, 10:46 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
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?
__________________
Reply With Quote
  #6  
Old 10-26-2011, 10:52 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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]; 
__________________
Quote:
Reply With Quote
  #7  
Old 10-26-2011, 10:54 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
Quote:
Originally Posted by fowlplay4 View Post
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
__________________
Reply With Quote
  #8  
Old 10-27-2011, 12:39 AM
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
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?
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #9  
Old 10-27-2011, 01:41 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by xXziroXx View Post
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.
__________________
Quote:
Reply With Quote
  #10  
Old 10-27-2011, 02:37 AM
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 fowlplay4 View Post
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.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #11  
Old 11-04-2011, 07:42 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
Ok will fix that behaviour of randomstring(), it was originally not made for getting arrays as input.
Reply With Quote
  #12  
Old 11-04-2011, 07:52 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by Stefan View Post
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.
__________________
Quote:
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 11:50 AM.


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