- Joined
- Jan 12, 2017
- Messages
- 58
- Points
- 0
Hello all,
This is a function that I wrote in both Groovy and JavaScript that generate random Id number. This is one of the really great script, it show how you can deal with generate a random text string, and by having it in both version, it show an example of how you can port your source code into difference scripting format.
Both version of this script will do exactly the same thing, it is just whether which language you would prefer to use. The concept is that you have a string of alphabet from a-z and then 0-9 to generate a random value from. Each character in this string have a position. Therefore you generate a random number with the minimum is 0 and maximum is the max length of the string. You use this number to get a single alphabet character. For every random alphabet letter needed you just have to put it in an array and later turn that array back into a string, and you would have a string full of random alphabet letter or number. This is depend on what you would like to generate.
So what does this code do? It will take in four value, the first 3 value is the none random id number that you would like it to have. Each value is for a block of none random number. None of which can have negative. The fourth value is the length of each id block. This will be fill in with 0 to make up the complete block where your Id number is not large enough to fill the length. The minimum length is 2. If no length is given the script default value is 5. You can skip out the second and third value block simply passing it a 0 value or an empty string.
An example output would be like this idGenerator(1,0,0,5) would produce xxxxx-00001 -> in this example where xxxxx will be five random characters.
idGenerator(1,3,10,4) would produce xxxx-0001-xxxx-0003-xxxx-0010 -> in this example where x are will be replace with a random character.
This script is great for producing very secure user Id where one user cannot guess another but still give you the advantage where an admin can associate which Id belong to which group of user or department.
This code below is the JavaScript version:
This code below is the Groovy version:
The below is the PHP version:
This is a function that I wrote in both Groovy and JavaScript that generate random Id number. This is one of the really great script, it show how you can deal with generate a random text string, and by having it in both version, it show an example of how you can port your source code into difference scripting format.
Both version of this script will do exactly the same thing, it is just whether which language you would prefer to use. The concept is that you have a string of alphabet from a-z and then 0-9 to generate a random value from. Each character in this string have a position. Therefore you generate a random number with the minimum is 0 and maximum is the max length of the string. You use this number to get a single alphabet character. For every random alphabet letter needed you just have to put it in an array and later turn that array back into a string, and you would have a string full of random alphabet letter or number. This is depend on what you would like to generate.
So what does this code do? It will take in four value, the first 3 value is the none random id number that you would like it to have. Each value is for a block of none random number. None of which can have negative. The fourth value is the length of each id block. This will be fill in with 0 to make up the complete block where your Id number is not large enough to fill the length. The minimum length is 2. If no length is given the script default value is 5. You can skip out the second and third value block simply passing it a 0 value or an empty string.
An example output would be like this idGenerator(1,0,0,5) would produce xxxxx-00001 -> in this example where xxxxx will be five random characters.
idGenerator(1,3,10,4) would produce xxxx-0001-xxxx-0003-xxxx-0010 -> in this example where x are will be replace with a random character.
This script is great for producing very secure user Id where one user cannot guess another but still give you the advantage where an admin can associate which Id belong to which group of user or department.
This code below is the JavaScript version:
Code:
// This is the JavaScript version.
function idGenerator(n1, n2, n3, lead0){
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 1)) {s = "0" + s;}
return s;
}
if (typeof(n1) === 'undefined') n1 = 1;
if (typeof(n2) === 'undefined') n2 = 0 ;
if (typeof(n3) === 'undefined') n3 = 0 ;
if (typeof(lead0) === 'undefined') lead0 = 5;
if (n1 < 0 || n2 < 0 || n3 < 0 || lead0 < 0){var error = "can't work with negative"; return error;};
if (n2 < 1 && n3 > 0){var error = "input have to follow order" ; return error;};
if (lead0 < 2){var error="need at least 2"; return error;};
var n1 = n1 === 0? 1 : n1;
var alpha = "abcdefghijklmnopqrstuvwxyz123456789";
var idstr = "";
n1out = (n1).pad(lead0); n2out = (n2).pad(lead0); n3out = (n3).pad(lead0);
rdblk = 0;
rdarr = [];
if (n1 > 0){rdblk = rdblk +1}; if (n2 > 0){rdblk = rdblk +1}; if (n3 > 0){rdblk = rdblk +1};
for (i = 0; i < rdblk; i++) {
var a = 0;
var b = "";
while (a < lead0){
b += alpha.charAt(getRandom(0, alpha.length) );
a++;
}
rdarr[i] = b;
}
if (rdblk == 3){idstr = rdarr[0]+ "-" +n1out+ "-" +rdarr[1]+ "-" +n2out+ "-" +rdarr[2]+ "-"+n3out};
if (rdblk == 2){idstr = rdarr[0]+ "-" +n1out+ "-" +rdarr[1]+ "-" +n2out};
if (rdblk == 1){idstr = rdarr[0]+ "-" +n1out};
return idstr;
}
Code:
// Groovy version
​public idGenerator(int n1 = 0, int n2 = 0, int n3 = 0, int lead0 = 5){
if (n1 < 0 || n2 < 0 || n3 < 0 || lead0 < 0){return "can't work with negative"}
if (n2 < 1 && n3 > 0){return "input have to follow order"}
if (lead0 < 2){return "need at least 2"}
n1 = (n1 == 0)? 1 : n1
format = "%0"+lead0+"d"
String alpha = "abcdefghijklmnopqrstuvwxyz123456789"
String idstr
n1out = String.format(format, n1); n2out = String.format(format, n2); n3out = String.format(format, n3)
rdblk = 0
rdarr = []
if (n1 > 0){rdblk = rdblk +1}; if (n2 > 0){rdblk = rdblk +1}; if (n3 > 0){rdblk = rdblk +1}
for (i = 0; i < rdblk; i++) {
rdarr[i] = new Random().with { (1..lead0).collect { alpha[nextInt( alpha.length() ) ] }.join()}
}
if (rdblk == 3){idstr = rdarr[0]+ "-" +n1out+ "-" +rdarr[1]+ "-" +n2out+ "-" +rdarr[2]+ "-"+n3out}
if (rdblk == 2){idstr = rdarr[0]+ "-" +n1out+ "-" +rdarr[1]+ "-" +n2out}
if (rdblk == 1){idstr = rdarr[0]+ "-" +n1out}
return idstr
}
The below is the PHP version:
Code:
<?php
function idGenerator($n1 = 0, $n2 = 0, $n3 = 0, $lead0 = 5){
if ($n1 < 0 || $n2 < 0 || $n3 < 0 || $lead0 < 0){ return "can't work with negative";}
if ($n2 < 1 && $n3 > 0){return "input have to follow order";}
if ($lead0 < 2){return "need at least 2";}
$n1 = ($n1 == 0)? 1 : $n1;
$format = "%'.0".$lead0."d";
$alpha = "abcdefghijklmnopqrstuvwxyz123456789";
$idstr;
$n1out = sprintf($format, $n1); $n2out = sprintf($format, $n2); $n3out = sprintf($format, $n3);
$rdblk = 0;
$rdarr = array();
if ($n1 > 0){$rdblk = $rdblk +1 ;}; if ($n2 > 0){$rdblk = $rdblk +1;}; if ($n3 > 0){$rdblk = $rdblk +1 ;};
$rdstr = "";
for ($i = 0; $i < $rdblk; $i++) {
for ($id = 0; $id < $lead0; $id++){
$rdstr = $rdstr . $alpha[rand(0,strlen($alpha)-1)];
}
$rdarr[$i] = $rdstr; $rdstr = "";
}
if ($rdblk == 3){$idstr = $rdarr[0]. "-" .$n1out. "-" .$rdarr[1]. "-" .$n2out. "-" .$rdarr[2]. "-".$n3out;};
if ($rdblk == 2){$idstr = $rdarr[0]. "-" .$n1out. "-" .$rdarr[1]. "-" .$n2out;};
if ($rdblk == 1){$idstr = $rdarr[0]. "-" .$n1out;};
return $idstr;
}
?>