“Zufällige Zeichenfolge” Code-Antworten

Python Random String

import random
import string

def random_string_generator(str_size, allowed_chars):
    return ''.join(random.choice(allowed_chars) for x in range(str_size))

chars = string.ascii_letters + string.punctuation
size = 12

print(chars)
print('Random String of length 12 =', random_string_generator(size, chars))
VasteMonde

Python Random String

import secrets 
secrets.token_hex(nbytes=16)

# this will produce something like 
# aa82d48e5bff564f3221d02194611c13
Danny Mor

Generieren Sie zufällige String -Python

import string
import random

length=5
#python2
randomstr = ''.join(random.sample(string.ascii_letters+string.digits,length))


#python3
randomstr = ''.join(random.choices(string.ascii_letters+string.digits,k=length))

                                  
Worrisome Wallaby

zufällige String -Methode Java

import java.util.Random;

public class Generator {
	public static String generateRandomPassword(int len) {
		String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk"
          +"lmnopqrstuvwxyz!@#$%&";
		Random rnd = new Random();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++)
			sb.append(chars.charAt(rnd.nextInt(chars.length())));
		return sb.toString();
	}
}
Vishal

Zufällige Zeichenfolge

private static Random random = new Random();

public static string RandomString(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return new string(Enumerable.Repeat(chars, length)
        .Select(s => s[random.Next(s.Length)]).ToArray());
}
PrashantUnity

Zufällige Zeichenfolge

// this one is quite compact and generates 11 random letters+numbers
Math.random().toString(36).substring(2) // random output like "0r4q9s1how7"
// just change substring() from 2 to 11 to reduce the string
Your Friendly Naked Dragon Seath

Ähnliche Antworten wie “Zufällige Zeichenfolge”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen