Skip to content

Instantly share code, notes, and snippets.

@maacpiash
Last active April 24, 2018 18:16
Show Gist options
  • Save maacpiash/50d493f9d273095f31b821a424face35 to your computer and use it in GitHub Desktop.
Save maacpiash/50d493f9d273095f31b821a424face35 to your computer and use it in GitHub Desktop.
A program to shuffle the lines of a text file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>
using System;
using System.IO;
using static System.Console;
using static System.IO.File;
namespace ListShuffler
{
class Program
{
static void Main(string[] args)
{
string fileName;
if(args.Length < 1)
{
WriteLine("Please enter a filename: ");
fileName = ReadLine();
}
else
fileName = args[0];
string[] inputLines, outputLines;
try
{
inputLines = ReadAllLines(fileName);
}
catch (Exception x)
{
WriteLine(x.ToString());
return;
}
int max = inputLines.Length;
outputLines = new string[max];
bool[] check = new bool[max];
// This is for keeping track of lines already included in the new array
Random r = new Random();
int count = 0, token;
while(count < max)
{
token = r.Next(0, max);
if(!check[token]) // Means this line has not been used
{
outputLines[count++] = inputLines[token];
check[token] = true;
}
}
WriteAllLines(fileName, outputLines);
}
}
}
7
1
9
3
6
8
5
4
2
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment