Using JavaScript to Create Random IDs
I wanted to share a quick solution I showed the other day for solving the following challenge: how to create a random ID (alphanumerical)? In this case, it was for a certificate.
- Create a text variable in Storyline (let’s say randomID)
- Add a JavaScript trigger
- Copy the following JavaScript to create a 12 digit alphanumerical upper-case ID:
var x1 = Math.random().toString(36);
var x2 = Math.random().toString(36);
var x3 = x1.substr(2,6).toUpperCase()+x2.substr(2,6).toUpperCase();
var p = GetPlayer();
p.SetVar(“randomID”,x3);
-
Caveat: random does not mean unique. Using Math.random (with 6 digits) provides a number between 0 and 1 that passes the statistical test for randomness but after a certain number of draw, you may encounter duplicates. That’s why I used 12 digits. You can change the number of digits by changing the number in substr(2,6) from 6. You can set any number between 1 to 8 safely. That is the number of digits you will get in the ID.
Leave a Comment