# Idiom #317 Random string

Create a string s of n characters having uniform random values out of the 62 alphanumeric values A-Z, a-z, 0-9

``````function RandomString(Size: Integer): String;
const
Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
Len = Length(Chars);
var
i: Integer;
begin
Result := '';
SetLength(Result, Size);
for i := 1 to Size do
begin
Result[i] := Chars[Random(Len)+1];
end;
end; ``````
``import "math/rand"``
``````const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

func randomString(n int) string {
a := make([]byte, n)
for i := range a {
a[i] = alphanum[rand.Intn(len(alphanum))]
}
return string(a)
}``````
``import "math/rand"``
``````const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

func randomString(n int, rng *rand.Rand) string {
a := make([]byte, n)
for i := range a {
a[i] = alphanum[rng.Intn(len(alphanum))]
}
return string(a)
}``````
``import "math/rand"``
``````var alphanum = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

func randomString(n int, rng *rand.Rand) string {
a := make([]rune, n)
for i := range a {
a[i] = alphanum[rng.Intn(len(alphanum))]
}
return string(a)
}``````
``````const s = ((n) => {
const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let s = "";
for (let i = 0; i < n; i += 1) {
s += alphanum[~~(Math.random() * alphanum.length)];
}
return s;
})(n);``````
``import java.util.Random;``
``````public static String generateValues(int n)
{
String alphabet1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String alphabet2 = "abcdefghijklmnopqrstuvwxyz";
String digits = "0123456789";
String values = alphabet1 + alphabet2 + digits;

Random random = new Random(1);
String s = "";
for(int count = 0; count < n; count++) {
s += String.valueOf(values.charAt(random.nextInt(values.length())));
}

return s;
}``````
``````my @chars = ('A'..'Z', 'a'..'z', '0'..'9');
my \$s='';
\$s .= \$chars[int rand @chars] for 1..\$n;``````
``````import random
import string``````
``````alphanum = string.ascii_letters + string.digits
s = ''.join(random.choices(alphanum, k=n))``````
``require 'securerandom'``
``s = SecureRandom.alphanumeric(n)``
``````use rand::distributions::Alphanumeric;
use rand::Rng;``````
``````fn random_string(n: usize) -> String {
``use rand::distributions::{Alphanumeric, DistString};``
``````fn random_string(n: usize) -> String {