• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 765
  • Last Modified:

Random 10-digit mod 10 generator

Dear Experts,

I want to have a method for generation of Random 10-digit numbers with mod 10 validation.

3 Solutions
Naitik GamitSoftware DeveloperCommented:
try like this:

using System.Security.Cryptography; // Import this Dll
public string Get10Digits()
    var bytes = new byte[5];
    var rng = RandomNumberGenerator.Create();
    uint random = BitConverter.ToUInt32(bytes, 0) % 10000000000;
    return String.Format("{0:D10}", random);
also look at this link:
JimiJ13I T ConsultantAuthor Commented:
Thanks Mr. Gamit for a quick reply:

However, I got an error as follows:
Error - Cannot implicitly convert type 'long' to 'uint'. An explicit conversion exists (are you missing a cast?)      
from line:  BitConverter.ToUInt32(bytes, 0) % 10000000000;

Any idea?
bbaoIT ConsultantCommented:
10,000,000,000 seems out of the scope of an unsigned integer 32.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

JimiJ13I T ConsultantAuthor Commented:
When I changed the problematic line to this then it produces 10-digit numbers but not Modulus 10 format :

 long random = BitConverter.ToInt32(bytes, 0) % 10000000000;
A ten digit number mod 10 (% 10) is a one digit number.
If you want a ten digit number that passes a Luhn check, you can generate 9 random digits and then add a check digit according to https://bitbucket.org/multipetros/validation/src/e8f43334e067/Validation/Luhn.cs
Where instead of return (idSum%10 == 0) you add 10-idSum
JimiJ13I T ConsultantAuthor Commented:

Thanks! That makes sense.  Anyway, I have already the working solution I got from somewhere:
    public static bool Mod10Check(string creditCardNumber)
                //// check whether input string is null or empty
                if (string.IsNullOrEmpty(creditCardNumber))
                    return false;

                //// 1.      Starting with the check digit double the value of every other digit
                //// 2.      If doubling of a number results in a two digits number, add up
                ///   the digits to get a single digit number. This will results in eight single digit numbers                    
                //// 3. Get the sum of the digits
                int sumOfDigits = creditCardNumber.Where((e) => e >= '0' && e <= '9')
                                .Select((e, i) => ((int)e - 48) * (i % 2 == 0 ? 1 : 2))
                                .Sum((e) => e / 10 + e % 10);

                //// If the final sum is divisible by 10, then the credit card number
                //   is valid. If it is not divisible by 10, the number is invalid.            
                return sumOfDigits % 10 == 0;
JimiJ13I T ConsultantAuthor Commented:
Thanks for the try!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now