Posts

Showing posts with the label random

Pseudo-Random UUID Generation with mask support

There are a lot of cases where we may want to generate a pseudo- random universally unique identifier ( UUID ) , sometimes called GUID . I previously wrote an article on creating Pseudo Random numbers in JavaScript, which generates a uuid of length X where x is a numeric value supplied. http://pragmatic-coding.blogspot.com/2012/01/javascript-pseudo-random-id-generator.html The problem with the above generator is that is does not allow developers the flexibility of specifying the format of the uuid as shown below: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxx0' 'xxxx-xxxx-xxxx-4xxx-yxxx-xxxxxxxxxxx' 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxx-xxxxxx' Above I’ve shown you 3 different uuid formats that 3 different developers wanted, so an algorithm had to be devised that allow developers the flexibility of changing the data formats. One other thing to note about the above is the x values, which represents the values you want to be randomly generated. All o...

JavaScript Pseudo-Random ID Generator

Simple Random Id Generator for JavaScript Sometimes you might need a Random ID generator. The following generator takes an integer from which it will generate a string of that length. Longer string means more permutations and increased uniqueness. The following function was tested with input 15. A sample space of 5,000,000 IDs was generated and all were tested unique. function generateRandomId ( length ) {     "use strict" ;     var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" , returnValue = "" , x , i ;     for ( x = 0 ; x < length ; x += 1 ) {         i = Math . floor ( Math . random () * 62 );         returnValue += chars . charAt ( i );     }     return "ID_" + returnValue ; } NB: Increase length for increased uniqueness GenerateRando...