C
C#4mo ago
ChezZ

Generate a unique number given two other numbers between -10 and 10

Is this possible? I cannot seem to figure out an equation that doesn't have collisions.
6 Replies
blueberriesiftheywerecats
for this case the easiest way is to create dictionary that stores existing values or you want to make seed with 2 numbers?
ChezZ
ChezZOP4mo ago
I want to pass two numbers in and generate one single number
Salman
Salman4mo ago
how do you want the single number to be generated using those two numbers ? Like what's the purpose of those two nums
MODiX
MODiX4mo ago
blueberriesiftheywerecats
REPL Result: Success
var used = new List<int>();

for (int i = -10; i <= 10; i++)
{
for (int j = -10; j <= 10; j++)
{
used.Add(GenerateUniqueNumber(i, j));
}
}

Console.WriteLine(used.Count == used.Distinct().Count());

int GenerateUniqueNumber(int a, int b)
{
// Ensure numbers are within the range -10 to 10
if (a < -10 || a > 10 || b < -10 || b > 10)
{
throw new ArgumentException("Numbers must be between -10 and 10");
}

// Map numbers from range -10 to 10 to a positive range for uniqueness
int rangeOffset = 10; // Offset to map -10 to 0
int mappedA = a + rangeOffset;
int mappedB = b + rangeOffset;

// Generate unique number using a combination formula
int uniqueNumber = mappedA * 21 + mappedB; // 21 is the number of possible values for num2 (-10 to 10 inclusive)

return uniqueNumber;
}
var used = new List<int>();

for (int i = -10; i <= 10; i++)
{
for (int j = -10; j <= 10; j++)
{
used.Add(GenerateUniqueNumber(i, j));
}
}

Console.WriteLine(used.Count == used.Distinct().Count());

int GenerateUniqueNumber(int a, int b)
{
// Ensure numbers are within the range -10 to 10
if (a < -10 || a > 10 || b < -10 || b > 10)
{
throw new ArgumentException("Numbers must be between -10 and 10");
}

// Map numbers from range -10 to 10 to a positive range for uniqueness
int rangeOffset = 10; // Offset to map -10 to 0
int mappedA = a + rangeOffset;
int mappedB = b + rangeOffset;

// Generate unique number using a combination formula
int uniqueNumber = mappedA * 21 + mappedB; // 21 is the number of possible values for num2 (-10 to 10 inclusive)

return uniqueNumber;
}
Console Output
True
True
Compile: 482.749ms | Execution: 51.678ms | React with ❌ to remove this embed.
blueberriesiftheywerecats
this seems to be working (chatgpt)
ChezZ
ChezZOP4mo ago
wait what thats generated by chat gpt? that works btw thank you
Want results from more Discord servers?
Add your server