I am trying to create a serial generator with 5 rows of 4 numbers each, mixed with numbers and letters, that is: F452-4B85-6985-6W87-4H87 I have already searched the web and no results appear. I am interested in knowing how I can create this generator implementing javascript or jquery and that it creates unlimited serials.
<!-- por ahora solo tengo el boton -->
<button>Generar serial</button>
This involves several tasks, essentially getting a character pseudorandomly within a range of values.
For our case, we put the possible values in a variable.
charAt
To get the character at a certain position we use the String method .To get a random integer index within a range we use basic math:
Let p be a pseudo-array of size l and given a charAt function to get the ith element. Being x a random value that comes in the range [0..1] in R+.
To obtain an element y in the value space defined by p we define the functionValidIndex as:
and to get a valid element in the pseudo-array just use
The next thing I would do is get each block, that is, four random characters at a time. Or as functional is fashionable, we could say that the function we made is pure and by aggregation we can compose it to obtain a monad from the monoid and blah blah. At the moment this is not so relevant since we will not use threads or any of those fun things that few use in JS.
And finally join them with a template just for the fact that it looks prettier than an old school concatenation.