Random number generator in C# I don't know if it is any good. I've never done this, thus, I noticed it is kinda slow.

Can you suggest improvements? I may use it in actual software in the future.

private int secgen(int minvalue, int maxvalue)
    {
        Func&lt;int, int, int, int&gt; modular = (a, b, mod) =&gt; 
        {
            long rem;
            Math.DivRem((long)a + (long)b, (long)mod, out rem);

            if (rem &lt; minvalue)
                rem += minvalue;

            return (int)rem;
        };


        byte[] entropyBytes = new byte[257];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

        rng.GetBytes(entropyBytes);
        int value = entropyBytes[0];
        for (int i = 1; i &lt; 257; i++) 
        {
            value = modular(value, entropyBytes[i], maxvalue);


        }

        //MessageBox.Show(value.ToString());
        return value;
    }
You may improve performance:

I would use the modulo operator instead of a function call you aren't benefiting from 100%. Math.DivRem returns a value (quotient) that you don't use. Same result:

var rem = a + b % mod;


Saves you a few unboxing operations and method call

Func&lt;int, int, int, int&gt; modular = (a, b, mod) =&gt;
{
    var rem = a + b % mod;

    if (rem &lt; minvalue)
        rem += minvalue;

    return rem;
};


Saves half the time? http://pastebin.com/XTaLCQh4