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());
}
Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
function randStr(n) {
var str = '';
for (var i = 0; i < n; i++) {
str += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
return str;
}
// 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
<?php
declare(strict_types=1);
function randomStrGenerator(int $length): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
if ($length < 5) {
return 'inputed value must be greater than 4';
} else {
for ($i = 0; $i < $length; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
}
echo "Error : ".randomStrGenerator(4)."
"; //This will throw an error with what the function expects
echo "Length of 5: ".randomStrGenerator(5)."
"; // This will give us a random length of 5 strings
echo "Length of 10: ".randomStrGenerator(10)."
";//This will give us a random length of 10 strings
echo "Length of 20: ".randomStrGenerator(20)."
";//This will give us a random length of 20 strings
echo "Hope you find this helpful";
// Create a function for reusable perpose
const generateRandomString = (myLength) => {
const chars =
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890";
const randomArray = Array.from(
{ length: myLength },
(v, k) => chars[Math.floor(Math.random() * chars.length)]
);
const randomString = randomArray.join("");
return randomString;
};
// Try it
console.log(generateRandomString(10));
console.log(generateRandomString(30));
string STRING = "Test 1
Test 2
Test 3
Test 4
Test 5";
int webLines = STRING.Split('
').Length;
int lineNumber = UnityEngine.Random.Range(0, webLines - 1);
string lineText = STRING.Split('
').ElementAtOrDefault(lineNumber);
const randStr = (length, min, max) => String.fromCharCode(...Array(length).fill().map(() => Math.floor(Math.random() * (max - min + 1)) + min))
console.log(randStr(10, 65, 90));
console.log(randStr(10, 97, 122)) //output: "jxqjxqjxqj")); between lowercase a
// z (10 letters)