Here’s a C# coding problem that brings light to a very subtle pattern that is generally overlooked. I’ll post the solution/explanation after people attempt it:
Given the following function:
int InclusiveRandomIntegerBetween(int start, int end)
Create a function GetRandos
(and any accompanying functions/classes you feel are needed).
GetRandos
should return a collection of N
UNIQUE random numbers where the range of random numbers is between 1
and N*2
. Assume that negative values and other edge cases just causes the function to explode and destroy reality as we know it.
Example: GetRandos(10)
would return a collection of 10
random numbers between 1
and 20
(N*2
with N
being 10
). The ten random numbers in the collection must be unique. Keep in mind that InclusiveRandomIntegerBetween
does not guarantee unique numbers (it only guarantees that an integer value will be returned for the given range).
Be sure to try it out peeps, it'll make you a better Unity/C# dev!
Here is a starting point for you, with a online REPL here: https://repl.it/languages/csharp
Here is a starting point (assume that you can't change InclusiveRandomIntegerBetween
:
using System;
class MainClass {
static Random random = new Random();
public static int InclusiveRandomIntegerBetween(int start, int end)
{
return random.Next(start, end + 1);
}
public static void Main (string[] args) {
Console.WriteLine ("Hello World");
}
}