I am trying to create unique identifiers with a script I am writing in JavaScript. The constraints I am applying to these identifiers are the following:
- The first nine characters of the identifier must be a random sequence of digits. I am defining 'digits' as the set over {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.
- The last character of each identifier must be a random, single English uppercase letter. I am defining this set over {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z}.
I understand that true randomness isn't represented by any programming language. Some programming languages simply have a library function that simulates "randomness", but I don't think any language actually implements the idea of true randomness. Nevertheless, let's pretend that the JavaScript Math.random() function is the best implementation of randomness.
Here is the JavaScript code that I have so far:
// the numbers in this array represent the utf-8 codes for the uppercase letters A-Z
var uppercaseLetters = new Array(65,66,67,68,69,70,71,72,73,74,75,76,77,78,
79,80,81,82,83,84,85,86,87,88,89,90);
// http://stackoverflow.com/questions/16753876/javascript-button-to-pick-random-item-from-array
var randomElement = uppercaseLetters[Math.floor(Math.random() *
uppercaseLetters.length)];
document.write(String.fromCharCode(randomElement));
convertedElement = String.fromCharCode(randomElement);
// document.write("<br /> <br />");
// borrowed String.fromCharCode from some part of the Mozilla developer website
// you can also see the utf-8 character set on wikipedia:
// http://en.wikipedia.org/wiki/UTF-8
/* for(var index = 0; index < 26; index++) {
document.write(String.fromCharCode(uppercaseLetters[index]));
} */
document.write("<br /> <br />");
var result;
for(var count = 0; count < 9; count++) {
var randomNumber = Math.ceil((Math.random() * 10) - 1);
// var randomNumber = Math.ceil((Math.random() * 9)); may also work
document.write(randomNumber);
}
result += convertedElement;
document.write(result);
// http://stackoverflow.com/questions/1451009/javascript-infamous-loop-problem
/* function createDigitString() {
for(var count = 0; count < 9; count++) {
var randomNumber = Math.ceil((Math.random() * 10) - 1);
// var randomNumber = Math.ceil((Math.random() * 9)); may also work
document.write(randomNumber);
}
} */
And here is the HTML to display it:
<!DOCTYPE html>
<html>
<head>
<base href="file:///C:/Users/sillyname/" />
<script src="random-sequence.js"></script>
</head>
<body>
</body>
</html>
The two files are obviously stored in the same directory. So, this code should display a "random" 9-digit sequence, skip a few lines, and then display a "random" uppercase letter. Now, I want to combine the two by appending the letter to the number sequence. What is the best (or simplest) way to achieve this?
EDIT: I should also clarify that I want to maintain the two pieces of "random-simulating" code separately. In other words, I would like to store the final sequence in a variable while maintaining separability:
var finalSequence = nineRandomDigits + randomUppercaseLetter
EDIT: Alright, I think I have what I want thank everyone that contributed. Here is the final solution.
function randomUppercaseLetter() {
var letter = String.fromCharCode(64 + Math.ceil(Math.random() * 26));
return letter;
}
function randomNumber(len) {
var randomNumber;
var n = '';
for(var count = 0; count < len; count++) {
randomNumber = Math.ceil((Math.random() * 10) - 1);
n += randomNumber.toString();
}
return n;
}
// Usage:
var finalSequence = randomNumber(9) + randomUppercaseLetter();
document.write(finalSequence);
are simply there for readability. – brthomps Nov 26 '13 at 18:43