Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
4.17/5 (3 votes)
See more:
Hello,

I want the name of library in C# that generates random numbers with different distributions.

Thanks
Posted
Updated 31-Mar-11 17:18pm
v2

You may generate the number with the uniform distribution provided by the standard Random class and then obtain the correnspondig number of another distribution 'simply' equating the values of the two cumulative functions.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-13 13:02pm    
The idea is correct, but, look at the level of other answers. Do you think anyone (I don't know about OP who apparently knows better then those "experts") can understand your correct words?
Maybe it needs further explanation. Second half of the answer is really unclear. I voted 4 for now...
—SA
Xmen Real 7-Mar-13 13:19pm    
There is no need to comment a 2 years old answer just to get reputation points.
CPallini 7-Mar-13 13:42pm    
No, I know his honest interest.
Sergey Alexandrovich Kryukov 7-Mar-13 14:03pm    
Thank you very much, Carlo.

I came to this page tracing posts of one another member, led by his today's post. But I actually don't need any excuses. I reasonably assume that I can comment any post I choose to comment.

To Xmen: do you think you are more free than others, dear? How can you accuse other people on such a ridiculous reason?
You can a free to post your baseless accusations, but, by some reason, I should not comment all the mistake people do here, only for the sake of helping other people to get knowledge, it that how you think?
This is absolutely indecent, to tell you the least.

—SA
Xmen Real 7-Mar-13 21:16pm    
Nobody stops you from commenting, but why comment after years passed, I wouldn't mind if you have commented that time or downvoted me, but now ? AYFKM ?
You can use this:

int randomNumber(int min, int max)
{
 Random rd = new Random()
 return rd.Next(min,max);
}

Method will return a number from min to max


Regards,
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-13 12:56pm    
It won't work correctly this way. Random should be instantiated only once in the Application Domain. This is very important.
You can construct it in declared class, and instantiate it only once, etc.
Besides, it does not resolve the problem of required distribution.
Incorrect answer, should never be used.
—SA
 
Share this answer
 
Comments
wizardzz 1-Jun-11 16:02pm    
Isn't that pretty much what was posted a month ago?
 
Share this answer
 
Try this one!

THis generates a unique sequence without the need for a random seed.

http://msdn.microsoft.com/en-us/library/system.security.cryptography.randomnumbergenerator.aspx[^]

Code example for file encryption using a random salt for the Rijndael algorithm:

private void button2_Click(object sender, EventArgs e)
{
    // Generate the "IV" and the salt for the password
    RandomNumberGenerator rng = RandomNumberGenerator.Create();
    byte[] IV = new byte[16];
    byte[] salt = new byte[16];
    rng.GetNonZeroBytes(IV);
    rng.GetNonZeroBytes(salt);
    // Hash the password
    Rfc2898DeriveBytes passwordHasher = new Rfc2898DeriveBytes(_password, salt);
    byte[] key = passwordHasher.GetBytes(16);
    Rijndael rijndaal = new RijndaelManaged();
    rijndaal.IV = IV;
    rijndaal.Key = key;
    using (FileStream input = File.Open(_fileName, FileMode.Open))
    {
        using (StreamReader reader = new StreamReader(input))
        {
            using (FileStream output = File.Create(_fileName + ".crypted"))
            {
                using (CryptoStream encryptingStream =
                    new CryptoStream(output, rijndaal.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    // first write the salt and the IV
                    output.Write(salt, 0, 16);
                    output.Write(IV, 0, 16);
                    output.Flush();
                    using (StreamWriter writer = new StreamWriter(encryptingStream))
                    {
                        writer.Write(reader.ReadToEnd());
                    }
                }
            }
        }
    }
}


Happy coding ...

Regards, Perry
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-13 13:03pm    
Sorry, off-topic, maybe by accident. The only matching topic is "random"...
—SA
Perry Bruins 5-Jun-13 9:27am    
Sergey,

The code example is only there to show the USAGE of the RandomNumberGenerator class, which generates cryptograhically strong random numbers, what the OP asked for in my opinion.

By the way, this topix is already years old, so maybe your comment is of topic?

Regards, perry

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