- Home /
Random code generation
Hi, I have a fairly simple question to ask here.. Or, at least I hope it's simple.. What I want to do is have my .js script to generate a random authentication code and, for now, just print it to the console. So, something like this
"Random code: aAs12eIad091ueQp5Pq249"
would be printed. I think Random.Range() might do it, but I don't know how to make it print characters alongside the numbers.
Many thanks, TG106
Answer by Graham-Dunnett · Apr 17, 2012 at 12:11 PM
// Javascript example follows
private var characters : String = "0123456789abcdefghijklmnopqrstuvwxABCDEFGHIJKLMNOPQRSTUVWXYZ";
function Start () {
var code : String = "";
for (var i : int = 0; i < 20; i++) {
var a : int = Random.Range(0, characters.length);
code = code + characters[a];
}
Debug.Log(code);
}
This code uses Random.Range to pick a random character from a String that contains all the characters you want to allow in your authentication code. Note that if you want to be secure, you might be better off using a library that provides this kind of security.
Hm.. The Random.Range(0, characters); is giving me an error "BCE0023: No appropriate version of 'UnityEngine.Random.Range' for the argument list '(int, String)' was found" Any thoughts?
It's not really an issue, but lowercase y and z are missing from the string.
Answer by Davidovich · Apr 17, 2012 at 12:36 PM
A common way of doing this is to specify the character set for your random code in an array, then pull random items out of that array to build the string. Like so:
var desiredCodeLength = 15;
var code = "";
characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray();
while(code.Length < desiredCodeLength) {
code += characters[Random.Range(0, characters.length)];
}
Debug.Log("Random code: " + code);
You were on the right track with Random.Range()!
Shouldn´t it be ... code += characters[Random.Range(0, characters.length-1)]; ... to avoid an index out of bounds exception because Random.Range includes the limits in the result.
@$$anonymous$$odernAlchemist:
No Random.Range with int parameters is $$anonymous$$ includive and max exclusive. With float parameters $$anonymous$$ and max are theoretically both inclusive but that's such a rare case.
Also see this code snippet which tests that very property.
Answer by MrLucid72 · Aug 13, 2020 at 04:09 AM
Based off @Davidovich , but with slightly more efficiency, readability, better typings (instead of var) and .Length typo fixed wrapped in a static func:
/// <summary>
/// 0-9 A-Z a-z (Length of roomName === 6; up from 4).
/// https://answers.unity.com/questions/241219/random-code-generation.html
/// </summary>
/// <returns></returns>
public static string GenerateRandomAlphaNumericStr(int desiredLength)
{
StringBuilder codeSB = new StringBuilder(""); // Requires @ top: using System.Text;
char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray();
char singleChar;
while (codeSB.Length < desiredLength)
{
singleChar = chars[UnityEngine.Random.Range(0, chars.Length)];
codeSB.Append(singleChar);
}
Debug.Log("GenerateRandomAlphaNumericStr: " + codeSB.ToString());
return codeSB.ToString();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613070428im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Code to randomly generate a mesh? 0 Answers
how can i make randomly generated worlds with collectable resources 0 Answers
Infinite loop when I try to generate randomly a 2D dungeon.. 3 Answers
How can I make a Terraria Style game from sprites without using an enormous amount of gameobjects? 2 Answers
Random Island Generation 1 Answer