Solved

Random 10-digit mod 10 generator

Posted on 2015-02-16
8
516 Views
Last Modified: 2015-03-14
Dear Experts,

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


Thanks.
0
Comment
Question by:JimiJ13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 8

Assisted Solution

by:Naitik Gamit
Naitik Gamit earned 150 total points
ID: 40611929
try like this:

using System.Security.Cryptography; // Import this Dll
public string Get10Digits()
{
    var bytes = new byte[5];
    var rng = RandomNumberGenerator.Create();
    rng.GetBytes(bytes);
    uint random = BitConverter.ToUInt32(bytes, 0) % 10000000000;
    return String.Format("{0:D10}", random);
}
also look at this link:
http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx
0
 

Author Comment

by:JimiJ13
ID: 40611942
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?
0
 
LVL 37

Assisted Solution

by:bbao
bbao earned 100 total points
ID: 40611951
10,000,000,000 seems out of the scope of an unsigned integer 32.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JimiJ13
ID: 40611953
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;
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 40636933
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
0
 

Author Comment

by:JimiJ13
ID: 40665796
ozo,

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')
                                .Reverse()
                                .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;
            }
0
 

Author Closing Comment

by:JimiJ13
ID: 40665800
Thanks for the try!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question