Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a requirement to generate the 8 digit random number in C#3.5.I do not want to use the syste.random class since the random number can predictable.

So I used Cryptographic random number genearation but this gives me always random bytes only.Iam expecting an 8 digit random number from these random bytes.Is there any possibility getting only possitive random bytes and 8 digit random number from these random bytes.

Thanks,
Uday
Posted

First of all, I don't think System.Random gives predictable results. If you say so, would you please demonstrate your prediction? However, I'm not sure if the result of random generation is not correlated and obey required distribution. Even if it not perfect, it does not mean predictability.

Now, let's assume you have random byte. Let's say your method is byte Random().

C#
int RandomInt() {
    byte numBytes = sizeof(int);
    int value = 0;
    for (int index = 0; index < numBytes; index ++)
       value |= Random() << index * 8;
    return value;
}


Same thing with any other integer type. All you need is to shift each byte by the number of bits need to put it in all byte positions in sequence and OR with previous result. As number of bytes is calculated from the target type, it will work no matter what type you want to use as a target but... to write a generic (in a simple way) is nearly impossible.

—SA
 
Share this answer
 
v3
Comments
wizardzz 2-Feb-11 21:58pm    
I agree, I'm not sure why Random is not suitable without a better explanation from the OP
Sergey Alexandrovich Kryukov 2-Feb-11 22:07pm    
Thank you.
Chances are, these concerns are because of rumors "they say something's wrong with random". Another reason: seeding is often used incorrectly (I saw some example here in Questions and Answers), and then the patient blames the generator. In real life, quality of the generator can be very important, for example, in numeric simulations where statistics is derived from a numeric experiment. In other cases, it does not make big difference: solutions of equations using Monte-Carlo method for example: high precision of distribution is not so critical; it is also not critical in... security -- not at all. However, predictability would be fatal for security. In practice, this is not an issue.
--SA
aruidsp 2-Feb-11 23:28pm    
Hi SA,

Thanks a lot for the code.

The field value never assigned any value.
is it as below

index |= Random() << index * 8;

value = index;
return value;

Thanks,
Uday
Sergey Alexandrovich Kryukov 3-Feb-11 0:09am    
Sorry, I just fixed a bug, please see the update (the modification you provided did not fix it yet).
--SA
aruidsp 3-Feb-11 9:26am    
Hi SA,

Thanks for the update.
I have some issue with the code.
here is my code.

byte Random()
{
RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
byte[] randomBytes = new byte[4];
random.GetBytes(randomBytes);
return randomBytes;
}


public int randomint()
{
byte numBytes = sizeof(int);
int value = 0;
for (int index = 0; index < numBytes; index++)
value |= Random() << index * 8;

return value;
}


Now the randomBytes can not convert to byte.

Thanks.
Oh! Please concentrate, look at the interface and types properly!

To answer follow-up question:

C#
int RandomInt(RNGCryptoServiceProvider random) {
    byte numBytes = sizeof(int);
    byte randomBytes = byte[numBytes];
    random.GetBytes(randomBytes);
    int value = 0;
    for (int index = 0; index < numBytes; index ++)
       value |= randomBytes[index] << index * 8;
    return value;
}


Didn't you see the difference between byte[] and byte?! How could they convert?
Don't implement my Random, it was just for example, make random all numBytes (==4) bytes at once (shown above).

—SA
 
Share this answer
 
v3
Comments
aruidsp 4-Feb-11 21:34pm    
Hi SA,

Thanks.

But it returns more than 8 digit random number.
I assigned numbytes = 4 and run my code, it returns more than 8 digits.
But I need always an 8 digit random number only.

Thanks.
Sergey Alexandrovich Kryukov 4-Feb-11 21:46pm    
What code "returns"? I eliminated all functions returning arrays above.
Don't say "digits"! The integer types are not composed of digits. May be here is a big misconception. Now I'm not sure you understand computer presentation of integers and mix it up with string presentation ("digits" only exist in string presentation, int32 and uint32 are always 4 bytes).
If you're not going to understand that, nobody will help you.

Now, please concentrate and read carefully. The code above is correct and returns random integer byte-by-byte (not digit by digit). If you need that (8 digits), you should explain why very accurately. You should also define what a digit is, it depend (it could be decimal, hexadecimal, etc.)

The above code does not have a constant which defines the size of data. It is calculated by the type of parameter. If you change type to any other integer type, the number of bytes will be 1, 2, 4, or 8.

--SA
Maybe you could use part of this article where it generates a random encryption key:

Create and Share (with a client app) a Random Encryption Key[^]
 
Share this answer
 
You can use it
long randNum = 0;
Random random = new Random();
for (int i = 1; i <= 8; i++)
{
    long t = 0;
    if (i == 1)
        t = random.Next(1, 9);
    else
        t = random.Next(0,9);
    randNum = randNum * 10 + t;
}
Console.WriteLine(randNum);
 
Share this answer
 
Comments
aruidsp 2-Feb-11 23:29pm    
Thanks for code but I don't want use the random class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900