Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to get random string from array

Discussion in 'Scripting' started by ariusdb222, Mar 2, 2015.

  1. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    I tried to make a small script that will pick a random string from array and print it.
    Doesn't seem to work though...

    here's the code(if you can call it code)
    and no, i'm not very into programming....o_O


    Code (CSharp):
    1.  
    2. public class newbiome : MonoBehaviour {
    3.     string[] biomes = new string[] { "grasslands", "rainforest", "desert", "rocky", "swamp", "tundra"};
    4.     System.Random random = new System.Random();
    5.     int useBiome = random.Next (6);
    6.     string[] pickBiome = random[useBiome];
    7.     print(pickBiome);
    8.     return;
    9. }
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1. string randomString = biomes[Random.Range (0, biomes.Length);
     
    Justice0Juic3 and imranmollajoy like this.
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Looks like you're on the right track, but you have a couple of problems.

    First, you can't just dump executable code directly into the body of the class like that; it needs to be inside a function.

    Secondly, you want to use the random number as an index into the biomes array, not the random object.

    Also, right now you're choosing a random number less than 6. Rather than just putting a number here, it's a good idea to use the length of the array, as @DanielQuick suggests--that way, if you ever change the number of biomes, the random number generation will update automatically, so you don't need to remember to change it separately. This also helps anyone else who reads your code to know what you were thinking, whereas if they just see "6", they might not immediately realize that you chose the number 6 because it's the number of biomes in your array. (And when I say "anyone else who reads your code", that includes "you, six months from now when you've forgotten what you were doing.)

    Try this:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RandomBiome : MonoBehaviour {
    4.     void Start () {
    5.         string[] biomes = new string[] { "grasslands", "rainforest", "desert", "rocky", "swamp", "tundra"};
    6.         System.Random random = new System.Random();
    7.         int useBiome = random.Next (biomes.Length);
    8.         string pickBiome = biomes[useBiome];
    9.         print(pickBiome);
    10.     }
    11. }
     
  4. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Thanks for the reply, and the fixed code.. works perfectly:)
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I would suggest using Unity's Random function for most applications. System.Random has a few known issues. Most notably its not random if you create multiple new instances in quick succession.

    I can see @Antisone's code failing if there were several instances of the script created in the same frame.

    (Or it might not, Unity might be too slow for this to matter.)
     
  6. MUHAMMAD005

    MUHAMMAD005

    Joined:
    Sep 7, 2021
    Posts:
    2

    It should be biomes.Length-1
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,472
    No, it should not.

    For the integer version of Random.Range the upper bounds is exclusive while the lower bounds is inclusive. It's actually the same for System.Random's Next method.

    So you necro'd this 6 years old thread for nothing :)
     
    Kurt-Dekker likes this.
  8. VZPX

    VZPX

    Joined:
    Sep 5, 2021
    Posts:
    14
    how can you use that script but make it so it randomizes only once, i have a script that updates each time and that string updates each time so the string is constantly changing, is there a way to make the string randomizer only randomize once?