Created
March 1, 2017 22:14
-
-
Save pgsin/0d0d8f60b36985d2b88ad59b97edc0a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
namespace RandSeed { | |
class Program { | |
static void Main() { | |
string seeds, ms, ns; | |
int seed, m, n; | |
Console.Write("Seed: "); | |
seeds = Console.ReadLine(); | |
Console.Write("Range from 0 until: "); | |
ms = Console.ReadLine(); | |
Console.Write("Amount: "); | |
ns = Console.ReadLine(); | |
if (int.TryParse(seeds, out seed) && int.TryParse(ms, out m) && int.TryParse(ns, out n)) { | |
int[] res = GetRandomSet(seed, m, n); | |
if (res == null) { | |
throw new Exception("Wrong parameters"); | |
} else { | |
Console.Write("Output directory: "); | |
string dir = Console.ReadLine(); | |
if (Directory.Exists(dir)) { | |
string file; | |
do { | |
file = Path.Combine(dir, Path.GetFileNameWithoutExtension(Path.GetRandomFileName())); | |
} while (File.Exists(file)); | |
try { | |
using (StreamWriter sw = File.CreateText(file)) { | |
sw.WriteLine($"#{seed}\t{m}\t{n}"); | |
int j = 0; | |
for (int i = 0; i < m; i++) { | |
if (j != res.Length && res[j] == i) { | |
sw.WriteLine(1); | |
j++; | |
} else { | |
sw.WriteLine(0); | |
} | |
} | |
} | |
} catch (Exception e) { | |
throw new Exception(e.Message); | |
} | |
Console.WriteLine($"Successfully wrote to '{file}'"); | |
} else { | |
throw new Exception("Folder doesn't exist"); | |
} | |
} | |
} else { | |
throw new Exception("Not int parameters"); | |
} | |
Console.ReadKey(); | |
} | |
/// <summary> | |
/// Random generator | |
/// </summary> | |
private static int[] GetRandomSet(int seed, int m, int n) { | |
if (m < n || n < 1) { | |
return null; | |
} | |
int[] res = new int[n]; | |
Random rnd = new Random(seed); | |
for (int i = 0; i < n; i++) { | |
res[i] = rnd.Next() % (m - i); | |
} | |
Array.Sort(res); | |
for (int i = 1; i < n; i++) { | |
if (res[i - 1] >= res[i]) { | |
res[i] = res[i - 1] + 1; | |
} | |
} | |
return res; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment