- Home /
How to generate a random int that does not repeat consecutively?
Here 's my code. This is designed to get an integer to get the element of an array and use the string as Tags for spawning an object. However, my problem is it usually repeats the random int 3 times. I wanted to limit the repetition to 2 times consecutively only.
private string[] FirstTags = new string[3]
{
"a", "b", "c"
};
private string[] SecondTags = new string[5]
{
"v", "w", "x", "y", "z"
}
int randomInt_FirstTags;
int randomInt_SecondTags;
void GetRandomTag()
{
randomInt_FirstTags = Random.Range(0, FirstTags.Length);
randomInt_SecondTags = Random.Range(0, SecondTags.Length);
}
This code is where I call GetRandomTag() and spawn my objects. It is called every time the player scores.
public IEnumerator SpawnObjects()
{
GetRandomTag();
yield return new WaitForSeconds(0.8f);
SpawnFirstTags();
SpawnSecondTags();
}
I'm still a newbie in programming so bare with my codes. I badly need help with this. Thank you in advance!! :)
Answer by Llama_w_2Ls · Aug 18, 2020 at 09:06 AM
Well you could have another int called PreviousInt, which would be set to the random int generated every time. Then, if the int generated equals the PreviousInt (meaning that you generated the same number twice in a row), then you can keep regenerating the number until it equals something different. For example:
int PreviousWord;
void NewWord()
{
Start:
int RandomWord = Random.Range(0, 10);
if (RandomWord == PreviousWord)
{
goto Start;
}
else
{
PreviousWord = RandomWord;
//Add any function that occurs from the Randomly generated int
}
}
@Llama_w_2Ls where should I call the function NewWord()? :(( $$anonymous$$y code gets the random int every time the player scores and not in Update()
Thats fine, call it whenever you score then. it doesnt have to be in the update
goto
? Seriously? You are using one of the worst feature C# decided to keep from 60/70 years old languages.
While we all probably agree that goto should be used with care, I don't really see a huge issue here (besides the label name "Start" and the unnecessary "else"). The goto statement should be avoided when the code flow becomes hard to follow. In such a flat hierarchy it's not really a problem. Under the hood IL code as well as actual machine code only has jumps and conditional jumps which is what goto resembles.
Apart from that I wouldn't use goto in such a case because a do {} while()
loop would make more sense
int RandomWord;
do
{
RandomWord = Random.Range(0, 10);
} while (RandomWord == PreviousWord);
PreviousWord = RandomWord;
// [ ... ]
Another sneaky way would be using normal while loop like this
int RandomWord = PerviousWord;
while (RandomWord == PerviousWord)
{
RandomWord = Random.Range(0, 10)
}
PerviousWord = RandomWord;
Though a bit more readable would be something like this
while (true)
{
RandomWord = Random.Range(0, 10)
if (RandomWord != PerviousWord)
break;
}
PerviousWord = RandomWord;
This communicates the intention better and doesn't do questionable things like RandomWord = PerviousWord
just to make the construct work. Out of all those examples, the goto solution is also quite clear. Don't fear this keyword,. Just use it wisely when appropriate. Have a look at this SO question and lood through the different answers. Yes, it's about C++ but that doesn't really matter because the same applies to any other language that has a goto equivalent instruction.
Just ignoring a language feature because some people say to never use it is a bad reason. It's more important to understand why. It's mainly the overuse of goto, especially in larger more complex code.
Thanks a lot Bunny, im always trying to find out whats the best coding practice for every specific situation. Ill definitely try that instead.