# Whats the best way to check a random number range for duplicates then do something?

Strange question, hope I can explain it right.

What I’m trying to achieve is, generate a random range of numbers between 0 and 9 in a 10 number range, count the duplicates of a particular number, then do something.

I currently have this working, though am not sure if this is the correct, or best way considering performance.

Say the number range is 1517314982, and there are 3 1’s, do something. if the number range is 1577314811, there are 4 1’s, do something.

Currently doing something like this.

``````            if (numberCount == 3 && number.Key == 1)
{
Do Something
}

else if (numberCount == 4 && number.Key == 1)
{
Do Something
}
``````

Works fine, but I want to check for all numbers and counts, like 3 2’s, 5 4’s, etc. That would be one long else if.

Hope I explained it right, cheers for any help.

If I understand your issue correctly, you want to do a definite function but chosen by a random number. You could create a switch in a coroutine, set a random variable number with “your Number = Random.Range(0,10)” , so from 0 to 9 (if you use “using System”, you have to take “UnityEngine.Random.Range(0,10)”) and then set the “yield return new WaitForSecond(10)” function, before execute your method.

All i’m really trying to do is to see if there is a better way of doing this.

``````            if (numberCount == 3 && number.Key == 1)
{
Do Something
}

else if (numberCount == 4 && number.Key == 1)
{
Do Something
}

else if (numberCount == 5 && number.Key == 1)
{
Do Something
}
``````

Times this by, well at least 50 times to check if 222, 2222, 22222, 333, 3333, 33333 ect.

I already generate the number range, store it, then check it.

Like, if I hit Space, it makes the random number 1227690923, which it does. I then want to check if the number 2 is in the that number range 3 times, if it is, do something. But I want to check all possible numbers, and its repeated count. Say if I hit Space again and the number is 7330912345, it has detected that there are 3 3’s, do something.

Is this what you want?:

``````public void Start()
{
bool isCorrect = ContainsMany(1125869, 1, 2); // true
bool isCorrect2 = ContainsMany(1125869, 1, 3); // false
}

public bool ContainsMany(int number, int numberToCheck, int amount)
{
var values = number
.ToString()
.ToCharArray()
.Select(a => (int)char.GetNumericValue(a))
.ToList();

int count = 0;
foreach (var value in values)
{
if (value == numberToCheck) count++;
}
return count == amount;
}
``````

I was gonna post to do it from an update loop and just have if statements for everything u want to do on specific situations, but its kinda more of the same looking at the anwsers…

there are probbaly better ways to create the gameplay you want, as you can see in the comments there is no real way to write this down to make it look clean (u will always need those 100+ functions if u need it to do 100+ things)

here is where my doubts come in… do u really need 100+ different things to happen? I feel like u are making a real difficult solution to a problem that might be quite easy.

you have not shared any context on what u are trying to create… it might help explaining what game u want to make or what behavior u want to fix… maybe make a new post?

either way, good luck