- Home /
How to randomize an array of Strings?
Hi there!
I've found for a long time and I didn't find anything!
What I need is to pick up randomly a "name" from an array:
var NameArray : String[];
NameArray[0] = "Name";
NameArray[1] = "Name";
NameArray[2] = "Name";
NameArray[3] = "Name";
How can I pick a name randomly from that array? Please, help!!
happy to help =]
just some tips on using this 'site (for ALL new users, not just @Rmurillo$$anonymous$$ ) :
post comments to answers by clicking "add new comment" , a window then opens to type in. (you can delete your comments too, so please delete the self-answer-comment you put on this question).
Also to mark a correct answer for future readers, click the 'Tick' under the 'Thumb' next to the answer. (even though I feel rewarded enough by your nice feedback).
Otherwise, no problem. Happy Coding =]
Answer by AlucardJay · Apr 07, 2012 at 03:59 AM
chosenName = NameArray[Random.Range(0, NameArray.length)];
EDIT :
Are you sure ?
make a new scene and a new script. attach the script to the camera, then copy and paste this into the script :
#pragma strict
var NameArray : String[] = ["Name 00", "Name 11", "Name 22", "Name 33"];
var chosenName : String;
function ChooseName()
{
chosenName = NameArray[Random.Range(0, NameArray.length)];
Debug.Log("chosenName " + chosenName);
}
function OnGUI()
{
if (GUI.Button(Rect(10, 10, 100, 35), "random")) {ChooseName() ;}
GUI.Box (Rect(120, 10, 100, 35), "" + chosenName);
}
That code is correct. If you still have a problem, something else is wrong.
@Rmurillo$$anonymous$$ I believe you may have misunderstood what @alucardj is specifying here ... yes, the output of the Random.Range() will be an int, however that int is then used to deter$$anonymous$$e a specific element of your chosenName array and this string is outputted. :) So, as @Berenger said, that code is most definitely correct. :D
I get confused with this Random.Range($$anonymous$$,max); the documentation says both $$anonymous$$ and max are inclusive, but I realized that the max is never given. Also, in other languages, the max is exclusive. So is the documentation wrong? I guess so. If the doc is right then the code needs to be NameArray[Random.Range(0, NameArray.length-1)];
You're correct. :) Trying to specify an element of an array with its .length value will cause an "Array index is out of range" because of the inclusion of an Element 0. So, yes, @alucardj, it should be NameArray[Random.Range(0, NameArray.length - 1)]; :P
EDIT
Sorry, my statement is incorrect. See below.
@fafase Are you Sure ?!
Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only).
http://unity3d.com/support/documentation/ScriptReference/Random.Range.html
Interesting, as I hadn't read this, only went from experience. So ya made me check, @$$anonymous$$leptomaniac :P . If you want to confirm test yourself, in the posted script delete the OnGUI function and change the ChooseName function to Update. It never returns an "Array index is out of range" . =]
Answer by RmurilloM · Apr 07, 2012 at 04:23 AM
Thanks for your answer!
I already did that, but chosenName is going to be a int. And what I need is the String that number has in the Array!
Thanks again!
Your answer
Follow this Question
Related Questions
Referencing private string for array name 2 Answers
Assigning array element to variable 1 Answer
Setting variable to random string from an array? 1 Answer
String Access problem 1 Answer