![]() |
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:
|
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:
|
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.
|
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? |
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:
|
Quote:
|
Quote:
|
Quote:
PHP Code:
|
Quote:
|
Ok will fix that behaviour of randomstring(), it was originally not made for getting arrays as input.
|
Quote:
"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.