Skip to content

Instantly share code, notes, and snippets.

@aloisdg
Created July 2, 2017 18:50
Show Gist options
  • Save aloisdg/78e33ed9b4c42b8d2ca220359e255b7e to your computer and use it in GitHub Desktop.
Save aloisdg/78e33ed9b4c42b8d2ca220359e255b7e to your computer and use it in GitHub Desktop.
This is a function for creating a valid-ish credit card number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CardGenerator
{
/*
* Original instructions https://www.nerdwallet.com/blog/credit-cards/credit-card-numbers-valid-website-luhn-algorithm/
*/
static class Extensions
{
public static long NextCard(this Random r)
{
//var start = r.LongRandom(1111111111111111, 9999999999999999);
int process = 0;
List<int> start = new List<int>();
while (process % 10 != 0)
{
start = new List<int>();
var work = new int[10];
for (int i = 0; i < 16; i++) start.Add(r.Next(1, 9));
start.CopyTo(work, 6);
work.DoMath(x => x * 2);
for (int i = 0; i < work.Count(); i++)
{
if (work[i] >= 10)
{
work[i] = work[i] % 10 + (int)(work[i] / 10);
}
}
process = (work.Sum() * 9) % 10 + added;
}
long ret = 0;
for (int i = 1; i <= 16; i++)
{
ret = ret + (i * start[i]);
}
return ret;
}
public static IEnumerable<T> DoMath<T>(this IEnumerable<T> e, Func<T, T> operation)
{
var o = new List<T>();
var a = e.ToArray();
for (int i = 0; i < e.Count(); i++) o.Add(operation.Invoke(a[i]));
e = o.AsEnumerable();
return o.AsEnumerable();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment