'Creating Random string containing over 25 characters of numbers and letters (upper and lower case)

How to create a Random string with over 25 characters of numbers and letters with XSLT?

Example: Khb34KXQ23ib34KDNBBE342nQE

My XSLT is like this:

<xsl:function name="kh:shortRandom">
        <xsl:sequence select="generate-id()"/>
    </xsl:function>
    
  
   <xsl:template match="/">
        <test>
            <randomId><xsl:value-of select="concat(kh:shortRandom(), kh:shortRandom(), kh:shortRandom(), kh:shortRandom())"/></randomId>
        </test>
    </xsl:template>

But the answer is always the same..(e1d1).. Because i call the function four times.. the answer is also four time. (e1d1e1d1e1d1e1d1)

I want to have a different character every time. A little bit like password generator but just with letters and numbers.

Tnx :)



Solution 1:[1]

In XSLT 3.0 (XPath 3.1) one can use the random-number-generator() function.

For XSLT 2.0 I would recommend using the random number functions of FXSL - see for example: "Casting the Dice with FXSL: Random Number Generation Functions in XSLT"

Using this, here is an implementation of the wanted random-string generation function:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="my">
 <xsl:import href="C:/CVS-DDN/fxsl-xslt2/f/random.xsl"/>
 <xsl:output method="text"/>
 
 <xsl:variable name="vSeed" select=
   "xs:integer(seconds-from-time(current-time()))
   * xs:integer(minutes-from-time(current-time()))
   * xs:integer(hours-from-time(current-time()))"/>

  <xsl:template match="/">
     <xsl:value-of select="my:randomStrings(25, 10, $vSeed)" separator="&#xA;"/>
  </xsl:template>

  <xsl:function name="my:randomStrings">
    <xsl:param name="pRandomLength" as="xs:integer"/>
    <xsl:param name="pNumResults" as="xs:integer" />
    <xsl:param name="pSeed" as="xs:integer"/>
    
    <xsl:variable name="vAlphaNum" select="'abcdefghijklmnopqrstuvwxyz0123456789'"/>

    <xsl:variable name="vNums">
       <xsl:call-template name="randomSequence">
          <xsl:with-param name="pSeed" select="$vSeed"/>
          <xsl:with-param name="pStart" select="1"/>
          <xsl:with-param name="pEnd" select="36"/>
          <xsl:with-param name="pLength" select="$pRandomLength*$pNumResults"/>
       </xsl:call-template>
    </xsl:variable>
    
    <xsl:sequence select=
     "for $vK in 1 to $pNumResults
        return
          string-join(for $i in
                            $vNums/*[position() gt ($vK -1)*$pRandomLength 
                                   and position() le $vK*$pRandomLength]
                                         /xs:integer(.)
                        return substring($vAlphaNum, $i, 1),
                      '')
          "/>
  </xsl:function>
 </xsl:stylesheet>

The function my:randomStrings(pRandomLength, pNumResults, pSeed) implemented above produces a sequence of random strings and has these three arguments:

  1. pRandomLength - the wanted length of each generated random string
  2. pNumResults - the wanted number of random strings to be generated
  3. pSeed - a seed for the random generator. Calling the function with different seeds will produce different results.

The code above calls the function to produce 10 random strings each with length 25. The seed is calculated from the current time and thus the result will be different each time the transformation is performed.

Here is one result:

dfxlnfsox0dsh31gqueggobvn
awzilhbjrpph6f6blq19vzut6
tcpz3nxww0pwlzj4se40c9i8d
vvp9splktrb6z0k7mp908bk6m
lwlviyc9altbe2mumrlwxbusj
pq1x2vgvu52bpn7ikpexlzfb8
adn4si67rednwtyv0txj8pjlw
197nlsrbe6z1smr4bla8quy74
dg5sepf8evna3dn0ixlnvessn
yv8k3p0medf2u90clyk81b5ow

And here is anoter:

et4r20w7kuq859yd8e7fmoaf9
tdpforsjh3hdm9txsb3evmua3
uxctsbirlc2ulkh7erhawy2r6
igkmw5uszv1lrls4jk2qw9z0l
f0vo1tdhcbwitn2l7aprgvyau
c6suuyz6t5ytxv014iobjnrcm
5b504myox88r6yqoqtf4p4jzf
flhjib0wgn8liu209fqqh3lm7
tctsvxnqphrt2gy4yidyljeug
rlk9ku61n900x7lmk9onybd3u

Note:

As noted, you need to have downloaded the FXSL library and you will need to set the href attribute of the <xsl:import> declaration above, to point to the exact location of the file system, where the imported stylesheet file resides.

Solution 2:[2]

const characters = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const charactersLength = characters.length;

const generate25letterrandomstring = () => {
    let randomString = '';
    for (let i = 0; i < 25; i++) {
        randomString += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    console.log(randomString);
    return randomString;
}

generate25letterrandomstring()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dimitre Novatchev
Solution 2 Dharman