Skip to content

Instantly share code, notes, and snippets.

@wirasetiawan29
Created October 21, 2014 04:19
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 wirasetiawan29/8211c7491015d8083e2a to your computer and use it in GitHub Desktop.
Save wirasetiawan29/8211c7491015d8083e2a to your computer and use it in GitHub Desktop.
int[] CreateNumber(int length)
{
Random random = new Random();
int[] digits = new int[length];
// For loop keeps default value of zero for
last slot in array
for(int i = 0; i < length - 1; i++)
{
digits[i] = random.Next(10);
}
int sum = 0;
bool alt = true;
for(int i = length - 2; i >= 0; i--)
{
if(alt)
{
int temp = digits[i];
temp *= 2;
if(temp > 9)
{
temp -= 9;
}
sum += temp;
}
else
{
sum += digits[i];
}
alt = !alt;
}
int modulo = sum % 10;
if(modulo > 0)
{
digits[length-1] = 10 - modulo;
}
// No else req'd - keep default value of
zero for digits[length-1]
return digits;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment