- Home /
Question is off-topic or not relevant
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.
Answer by TomArano_Dimenco · Sep 04, 2020 at 02:33 PM
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
Answer by BenWiller1989 · Sep 04, 2020 at 09:03 AM
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.
Answer by nigelpower · Sep 04, 2020 at 09:26 AM
Cheers for the reply.
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.
Well, It depends on what you want to achieve. There are a lot of ways, to make it better. First of all, since the number. Key is always 1, throw it out and put everything in a single if statement, asking for the key. Second, in you case, I would use a switch function. So "switch (randomInt) { case 1: " do stuff" break; case 2: And so on } Third: If you have a large amount of functions and numbers, use a loop function, looping through all the integer, and call an Invoke function. That would safe you a ton of space!
Cheers again for the reply.
edit to add - I forgot to mention I'm using System.Linq;, where I'm getting the count and key from.
number.Key will be any number 0-9. number.Key simply checks if that number is in the range. numberCount counts how many times that number is in the range.
else if (numberCount == 4 && number.Key == 2)
{
Do Something
}
else if (numberCount == 5 && number.Key == 3)
{
Do Something
}
Answer by ShadyProductions · Sep 04, 2020 at 10:11 AM
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'm just trying to see if there are duplicated numbers in my generated range, and do something if there are depending on the amount. There will be a lot of number groups to check, just trying to see if there is a better way instead of writing a else if for each possible count.
var numberGroups = numberPool.GroupBy(i => i);
foreach (var number in numberGroups)
{
int numberCount = number.Count();
if (numberCount == 3 && number.Key == 0)
{
Do Something
}
else if (numberCount == 3 && number.Key == 1)
{
Do Something
}
else if (numberCount == 3 && number.Key == 2)
{
Do Something
}
else
{
//nothing
}
}
Not really if you want to do something different for each case.