Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a good way to generate a string of random characters? [closed]

Tags:

string

c#

random

I found this snippet of code that generates a string of random characters.

But is there a more elegant/faster/more reliable way to do this? This seems to rely on the fact that the numbers 26-91 are valid characters given the current encoding.

/// <summary>
/// Generates a random string with the given length
/// </summary>
/// <param name="size">Size of the string</param>
/// <param name="lowerCase">If true, generate lowercase string</param>
/// <returns>Random string</returns>
private string RandomString(int size, bool lowerCase)
{
    StringBuilder builder = new StringBuilder();
    Random random = new Random();
    char ch;

    for(int i = 0; i < size; i++)
    {
        ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
        builder.Append(ch);
    }

    if(lowerCase)
        return builder.ToString().ToLower();

    return builder.ToString();
}
like image 955
John B Avatar asked Jun 10 '09 16:06

John B


People also ask

How do you get a string of random strings?

Using randomUUID() java. util. UUID is another Java class that can be used to generate a random string. It offers a static randomUUID() method that returns a random alphanumeric string of 32 characters.

Can we generate random string in Python?

We can Generate Random Strings and Passwords in Python using secrets. choice(). For Cryptographically more secure random numbers, this function of the secret module can be used as its internal algorithm is framed in a way to generate less predictable random numbers.

How do you generate a random string of a fixed length in go?

Use math/rand to Generate Random String of Fixed Length in Golang. Go's math/rand package provides pseudorandom number generation. We'll use the package in this example and pass a random string to receive the output of a fixed-length string.

How do you select a random character in a string?

Using this random module, different random strings can be generated. random. choice() is used to generate strings in which characters may repeat, while random. sample() is used for non-repeating characters.


1 Answers

I'd prefer to pass the Random instance into the method - then you can reuse the same instance multiple times, which is important if you need to generate lots of random strings in quick succession. However, I'd also modify it somewhat anyway:

public const string LowerCaseAlphabet = "abcdefghijklmnopqrstuvwyxz";
public const string UpperCaseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static string GenerateUpperCaseString(int size, Random rng)
{
    return GenerateString(size, rng, UpperCaseAlphabet);
}

public static string GenerateLowerCaseString(int size, Random rng)
{
    return GenerateString(size, rng, LowerCaseAlphabet);
}

public static string GenerateString(int size, Random rng, string alphabet)
{
    char[] chars = new char[size];
    for (int i=0; i < size; i++)
    {
        chars[i] = alphabet[rng.Next(alphabet.Length)];
    }
    return new string(chars);
}
  • There's no need to use a StringBuilder when you know the final length
  • Using Random.NextDouble() indicates a lack of knowledge of the Random class. (In particular Random.Next(int, int)
  • Creating a new Random on each call is likely to result in duplicate strings
  • Calling Convert.ToInt32 and Convert.ToChar seems ugly compared with just casting
  • Lower-casing afterwards seems pointless compared with picking lower case letters to start with
  • Providing an alphabet to pick from is a lot more flexible (with helper methods for common cases)
like image 147
Jon Skeet Avatar answered Oct 05 '22 07:10

Jon Skeet