Skip to content

Instantly share code, notes, and snippets.

@PsichiX
Created April 20, 2016 03:55
Show Gist options
  • Save PsichiX/b6734c35bd2fc2e16bb59994abe865be to your computer and use it in GitHub Desktop.
Save PsichiX/b6734c35bd2fc2e16bb59994abe865be to your computer and use it in GitHub Desktop.
WinnerRandomSelector
using System;
using System.Collections.Generic;
using System.IO;
namespace WinnerRandomSelector
{
class Program
{
static void Main(string[] args)
{
var path = args.Length > 0 ? args[0] : "";
var fixedTime = args.Length > 1 ? args[1] : "";
var fixedTimeType = args.Length > 2 ? args[2] : "utc";
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
var time = DateTime.UtcNow;
if (!String.IsNullOrEmpty(fixedTime))
{
time = DateTime.ParseExact(fixedTime, "yyyy-MM-dd@HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
if (fixedTimeType == "local")
time = DateTime.SpecifyKind(time, DateTimeKind.Local).ToUniversalTime();
}
var seed = (Int32)(time.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
var input = File.ReadAllText(path);
var lines = new List<string>(input.Split(new char[] { ' ', '\t', ',', ';', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));
var random = new Random(seed);
var i = 0;
while (lines.Count > 1)
{
var index = random.Next() % lines.Count;
if (index >= 0 && index < lines.Count)
{
Console.WriteLine("LOSER #" + (++i) + ": " + lines[index]);
lines.RemoveAt(index);
}
}
Console.WriteLine("WINNER: " + lines[0]);
Console.WriteLine("SEED: " + seed);
Console.WriteLine("UTC TIME: " + time);
Console.WriteLine("LOCAL TIME: " + DateTime.SpecifyKind(time, DateTimeKind.Utc).ToLocalTime());
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment