- Home /
Using Random.Range in an array only gives a random output once
I'm trying to create a system that generates a sentence based on a list of structures and words. I begin by calling a method in a generate script that picks a string from the structure list. From there, the structure list also picks a string from the words list to fit into the places it needs. But there seems to be a problem I'm not understanding in the structure list. It does successfully place a string into the spots it's called for. But recalling the "CreateSentence" method does not seem to generate a new word. It just spits out the first word it outputted for that particular string. Does anyone know why this is occurring? Is it a bug? Or just something I'm not understanding right? Here are some example scripts of what my code looks like
public class Generate : MonoBehaviour
{
public Text onScreenText;
public void CreateSentence()
{
string finalString = Structure.strucures[Random.Range(0, Structure.strucures.Length)];
onScreenText.text = finalString;
}
}
public class Structure
{
public static string[] strucures = new string[]
{
"Your look " + List.wordList[Random.Range(0, List.wordList.Length)] + " today.",
"You looked " + List.wordList[Random.Range(0, List.wordList.Length)] + " yesterday.",
"You will look " + List.wordList[Random.Range(0, List.wordList.Length)] + " tomorrow."
};
}
public class List
{
public static string[] wordList = new string[]
{
"nice",
"beautiful",
"okay",
"ugly",
"awful"
};
}
Answer by myzzie · Dec 06, 2020 at 10:06 AM
When you get the string from the static array, it wont call a new method everytime. It's already done that when the array was initialized. Because it's static, there's only one of it. So you basically get the same string over and over again.
This solves it
private static string[] prefixes = new string[]
{
"You look ",
"You looked ",
"You will look "
};
private static string[] suffixes = new string[]
{
" today",
" yesterday",
" tomorrow"
};
public static string GetRandom()
{
int index = Random.Range(0, prefixes.Length);
string prefix = prefixes[index];
string adjective = List.wordList[Random.Range(0, wordList.Length)]
string suffix = suffixes[index];
return prefix + adjective + suffix;
}
You can even keep a single array of structures and just insert the words later;
//We can use string.Format() to inject strings into other strings - the first element of a format will replace the symbol '{0}', the second '{1}', and so on.
public static string[] strucures = new string[]
{
"Your look {0} today.",
"You looked {0} yesterday.",
"You will look {0} tomorrow."
};
//If you want a random structure, you can just use this property - every time you access it you will get a different sentence
public static string NewStructure => string.Format (structures[Random.Range (0, structures.Length)], List.wordList[Random.Range (0, List.wordList.Length)]);
...
public void CreateSentence()
{
onScreenText.text = Structure.NewStructure;
}
Thank you @myzzie and @Namey5 I like both of these answers. But I can't figure out how to use them if I want to have random amount of words to be filled in. Take for example, we want to make a food item that has the food item, adjectives, and a that statement after words, but we want the adjectives and that statement to be optional. So something like "mouth-watering cheese burger that has extra ketchup" or "tenderized, well seasoned chicken breast" or even just "T-bone steak" How I did this before was have a list of all of these things, and call a method that would pick, choose which and how many adjectives and that statements were used and place everything for me, and just return the value. I'd also not want my array to follow the same structure every time. $$anonymous$$aybe sometimes it's "I want to eat (Food)" or "I want to sit at (Location) while I eat my (Food)". Would I be able to recreate this with a string.format system? Is it possible to remove the static from my lists and still make them easy to access if I have 10+ of them? Thanks for the help!
I found a solution that works for me, as you can call a method in a string format, I just do that and I have what I need. I don't like the fact I need to have a long list of these string formats, even when the chosen structure does not call for them. But it works, and I'm happy with the way it is and is only slightly inconvenient comparatively
It all comes down to how precise you want your text. I'm guessing you don't want to write an entire grammar library just to piece some words together. The most simplistic approach is to have each sentence prepared with slight variations of adjectives etc. Like, I am hungry/full. That was delicious/quite awful/not bad and like you said. I'd probably write it in text format and parse it though.
Answer by azaib2671 · Dec 07, 2020 at 01:27 PM
thank you for sharing through this i've solve my shop and save near me our go saveshop store problems.