samedi 15 août 2015

same random string generator in c# and javascript

i have this nice and easy function that generates random string in c#

public static class StringHelper
{
    private static Random _rng;
    private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static string RandomString(int size)
    {
        char[] buffer = new char[size];

        for (int i = 0; i < size; i++)
        {
            buffer[i] = _chars[_rng.Next(_chars.Length)];
        }
        return new string(buffer);
    }

    public static string GenerateRandomString(int seed, int size)
    {
        _rng = new Random(seed);

        var result = RandomString(size);

        return result;
    }
}

but i need the same function in javascript. in other words i need the same function generating random strings in c# and javascript with given seed. Of course i could rewrite it somehow by myself but i'm afraid of different random functions in these languages - so in some cases results may be different.




Aucun commentaire:

Enregistrer un commentaire