Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active September 24, 2018 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/c6754636d2eefcd62c4fd54aac3a177b to your computer and use it in GitHub Desktop.
Save amirrajan/c6754636d2eefcd62c4fd54aac3a177b to your computer and use it in GitHub Desktop.
DSoP Random Number Generation

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");
  }
}

There are two important concepts that the problem above is trying to bring light to. One is "folding" and the other is "value coersion". Below is a partial solution to the problem. Take note of the FoldLeft and Into extension methods. Try to create them.

using System;
using System.Linq;
using System.Collections.Generic;
using LambdasThatCSharpShouldFuckingHaveButDoesntSoIHaveToWriteItEveryGodDamnTime;

class Solution
{
    static IEnumerable<int> GetRandos(int count)
    {
        return Enumerable
                   .Range(1, count)
                   .FoldLeft(
                       new List<int>(),
                       (i, aggregate) =>
                           GetRando(
                               max: count * 2,
                               exclusions: aggregate).Into(aggregate));
    }

    static int GetRando(int max, IEnumerable<int> exclusions)
    {
        var i = InclusiveRandomIntegerBetween(1, max);
        if (exclusions.Contains(i)) return GetRando(max, exclusions);
        return i;
    }

    static Random random = new Random();
    static void Main(string[] args) { GetRandos(10).Each(Console.WriteLine); }
    static int InclusiveRandomIntegerBetween(int start, int end)
    {
        return random.Next(start, end + 1);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment