Topic
  • 10 replies
  • Latest Post - ‏2014-11-20T20:55:22Z by 6QMG_Elayaraja_Kathirvel
bda7215
bda7215
37 Posts

Pinned topic Random Number Generator?

‏2008-09-10T14:36:44Z |
Is there any way to generate a random number? I was looking through the docs all i could fine was something that generated random-bytes() couldn't think of a way to covert this to a number...

Thanks for any help
Byron
Updated on 2011-01-09T00:56:30Z at 2011-01-09T00:56:30Z by HermannSW
  • ashoon
    ashoon
    66 Posts

    Re: Random Number Generator?

    ‏2008-09-15T20:15:41Z  
    you could use the transaction id - that's a random number that DP generates for each transaction...
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: Random Number Generator?

    ‏2008-09-19T16:43:35Z  
    • ashoon
    • ‏2008-09-15T20:15:41Z
    you could use the transaction id - that's a random number that DP generates for each transaction...
    Sorry, it's not random it is an incremental counter. It also has finite bounds so will wrap if the uptime of the device is long enough. It also resets on reboot/restart. I think you have to generate a GUID (or several) and do some fancy math.

    Can you share the exact use case? We might have some pre-determined solutions for you.

    Corey
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: Random Number Generator?

    ‏2008-09-22T22:53:57Z  
    Have you tried random-sequence?

    xmlns:random="http://exslt.org/random"
    random:random-sequence()

    Regards,
    --Arif
  • vivekaries
    vivekaries
    54 Posts

    Re: Random Number Generator?

    ‏2011-01-07T18:02:22Z  
    Have you tried random-sequence?

    xmlns:random="http://exslt.org/random"
    random:random-sequence()

    Regards,
    --Arif
    Hi All,

    Can anyone suggest how can we generate a random number in a XSLT. It should be supported by XI50.

    It seems that EXSLT function random.random-sequence() is not supported.

    I have to select a random value from a list of values. Thought of generating a random number and using it to pick up one of the values in the list.
  • HermannSW
    HermannSW
    6065 Posts

    Re: Random Number Generator?

    ‏2011-01-09T00:50:07Z  
    Hi All,

    Can anyone suggest how can we generate a random number in a XSLT. It should be supported by XI50.

    It seems that EXSLT function random.random-sequence() is not supported.

    I have to select a random value from a list of values. Thought of generating a random number and using it to pick up one of the values in the list.
    Hi Vivek,

    > Can anyone suggest how can we generate a random number in a XSLT. It should be supported by XI50.
    >
    see below.

    > It seems that EXSLT function random.random-sequence() is not supported.
    >
    You are right, DataPower does not support random:random-sequence() from exslt.org:
    http://publib.boulder.ibm.com/infocenter/wsdatap/v3r8m1/index.jsp?topic=/xs40/extensionfunctions189.htm

    The main reason that DataPower does not support random:random-sequence() seems to be the signature of the function.
    The return type is "sequence of numbers":
    http://exslt.org/random/functions/random-sequence/index.html
    But sequences are a XPath 2.0 construct not supported by DataPower's XPath 1.0 implementation.

    > I have to select a random value from a list of values. Thought of generating a
    > random number and using it to pick up one of the values in the list.

    You may use my implementation of dp:random-sequence() from below.

    The basis of generating random data is dp:random-bytes()
    http://publib.boulder.ibm.com/infocenter/wsdatap/v3r8m1/index.jsp?topic=/xs40/extensionfunctions130.htm

    This is the sample for generating a nonce value:
    
    <xsl:variable name=
    "nonce" select=
    "dp:random-bytes(20)"/>
    


    Below stylesheet does not only contain the implementation of dp:random-sequence(), but also a little sample application.
    Using this stylesheet in server-to-client rule of a MPGW with pass-thru request type and backend URL
    http://home.ubalt.edu/ntsbarsh/Business-stat/otherapplets/Randomness.htm
    allows to check the quality of the generated random numbers.

    Find attached:
    • sampleData.xsl (see below)

    sampleData.xsl:
    
    <xsl:stylesheet version=
    "1.0" xmlns:xsl=
    "http://www.w3.org/1999/XSL/Transform" xmlns:dp=
    "http://www.datapower.com/extensions" xmlns:regexp=
    "http://exslt.org/regular-expressions" xmlns:func=
    "http://exslt.org/functions" extension-element-prefixes=
    "dp" >   <!-- input:  base64 encoded 4 
    
    byte sequence (eg. by 
    "dp:random-bytes(4)") 
    
    return: value between 0 and 1 represented by input --> <func:function name=
    "dp:scale4"> <xsl:param name=
    "bytes4"/>   <func:result select=
    "dp:radix-convert($bytes4,64,10) div 4294967296" /> </func:function>   <!-- Recursively create sequence of random values --> <func:function name=
    "dp:random-sequenceR"> <xsl:param name=
    "bytes"/>   <xsl:variable name=
    "junk"> <xsl:message dp:priority=
    "error">bytes(<xsl:value-of select=
    "$bytes"/>)</xsl:message> </xsl:variable>   <func:result> <xsl:
    
    if test=
    "$bytes != ''"> <r><xsl:value-of select=
    "dp:scale4(dp:substring-base64($bytes,1,4))"/></r>   <xsl:copy-of select=
    "dp:random-sequenceR(dp:substring-base64($bytes,5,string-length($bytes)))"/> </xsl:if> </func:result> </func:function>   <!-- input:  number of requested random values between 0 and 1 
    
    return: requested 
    "sequence" DP does not support XPath 2.0 sequences. So 
    
    return <r>num1</r><r>num2</r>... --> <func:function name=
    "dp:random-sequence"> <xsl:param name=
    "numberOfItems"/>   <xsl:variable name=
    "bytes" select=
    "dp:random-bytes(4*$numberOfItems)"/>   <func:result select=
    "dp:random-sequenceR($bytes)" /> </func:function>       <!-- input: $str, HTML data from http:
    //home.ubalt.edu/ntsbarsh/Business-stat/otherapplets/Randomness.htm $values, random values between 0 and 1 to insert into the INPUT fields of $str --> <func:function name=
    "dp:replaceData"> <xsl:param name=
    "str"/> <xsl:param name=
    "values"/>   <xsl:choose> <xsl:when test=
    "count($values/*) = 0"> <func:result select=
    "$str"/> </xsl:when> <xsl:otherwise> <xsl:variable name=
    "last" select=
    "count($values/*)"/>   <xsl:variable name=
    "rest"> <xsl:copy-of select=
    "$values/*[position() &lt; $last]"/> </xsl:variable> <xsl:variable name=
    "from" select=
    "concat('&lt;INPUT name=P',$last,' ([^v]*)value=&quot;([0-9.]*)&quot;[ ]*>')"/>   <xsl:variable name=
    "to" select=
    "concat('&lt;INPUT name=P',$last,' $1value=&quot;',$values/*[$last],'&quot;>')"/>   <xsl:variable name=
    "repl" select=
    "regexp:replace($str, $from, '', $to)"/>   <func:result select=
    "dp:replaceData($repl,$rest)"/> </xsl:otherwise> </xsl:choose> </func:function>   <!-- type=
    "String" dp:input- and dp:output-mapping 
    
    for dealing with HTML data. --> <dp:input-mapping href=
    "String.ffd" type=
    "ffd"/> <dp:output-mapping href=
    "String.ffd" type=
    "ffd"/>   <!-- Set Content-Type of response to text/html. Fill 20 INPUT fields with random values. --> <xsl:template match=
    "/Conversion/string">   <dp:set-http-response-header name=
    "'Content-Type'" value=
    "'text/html'"/> <dp:freeze-headers/>   <Conversion> <string> <xsl:value-of select=
    "dp:replaceData(.,dp:random-sequence(20))"/> </string> </Conversion> </xsl:template>   </xsl:stylesheet>
    


    Hermann.
  • HermannSW
    HermannSW
    6065 Posts

    Re: Random Number Generator?

    ‏2011-01-09T00:51:13Z  
    • HermannSW
    • ‏2011-01-09T00:50:07Z
    Hi Vivek,

    > Can anyone suggest how can we generate a random number in a XSLT. It should be supported by XI50.
    >
    see below.

    > It seems that EXSLT function random.random-sequence() is not supported.
    >
    You are right, DataPower does not support random:random-sequence() from exslt.org:
    http://publib.boulder.ibm.com/infocenter/wsdatap/v3r8m1/index.jsp?topic=/xs40/extensionfunctions189.htm

    The main reason that DataPower does not support random:random-sequence() seems to be the signature of the function.
    The return type is "sequence of numbers":
    http://exslt.org/random/functions/random-sequence/index.html
    But sequences are a XPath 2.0 construct not supported by DataPower's XPath 1.0 implementation.

    > I have to select a random value from a list of values. Thought of generating a
    > random number and using it to pick up one of the values in the list.

    You may use my implementation of dp:random-sequence() from below.

    The basis of generating random data is dp:random-bytes()
    http://publib.boulder.ibm.com/infocenter/wsdatap/v3r8m1/index.jsp?topic=/xs40/extensionfunctions130.htm

    This is the sample for generating a nonce value:
    <pre class="jive-pre"> <xsl:variable name= "nonce" select= "dp:random-bytes(20)"/> </pre>

    Below stylesheet does not only contain the implementation of dp:random-sequence(), but also a little sample application.
    Using this stylesheet in server-to-client rule of a MPGW with pass-thru request type and backend URL
    http://home.ubalt.edu/ntsbarsh/Business-stat/otherapplets/Randomness.htm
    allows to check the quality of the generated random numbers.

    Find attached:
    • sampleData.xsl (see below)

    sampleData.xsl:
    <pre class="jive-pre"> <xsl:stylesheet version= "1.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" xmlns:dp= "http://www.datapower.com/extensions" xmlns:regexp= "http://exslt.org/regular-expressions" xmlns:func= "http://exslt.org/functions" extension-element-prefixes= "dp" > <!-- input: base64 encoded 4 byte sequence (eg. by "dp:random-bytes(4)") return: value between 0 and 1 represented by input --> <func:function name= "dp:scale4"> <xsl:param name= "bytes4"/> <func:result select= "dp:radix-convert($bytes4,64,10) div 4294967296" /> </func:function> <!-- Recursively create sequence of random values --> <func:function name= "dp:random-sequenceR"> <xsl:param name= "bytes"/> <xsl:variable name= "junk"> <xsl:message dp:priority= "error">bytes(<xsl:value-of select= "$bytes"/>)</xsl:message> </xsl:variable> <func:result> <xsl: if test= "$bytes != ''"> <r><xsl:value-of select= "dp:scale4(dp:substring-base64($bytes,1,4))"/></r> <xsl:copy-of select= "dp:random-sequenceR(dp:substring-base64($bytes,5,string-length($bytes)))"/> </xsl:if> </func:result> </func:function> <!-- input: number of requested random values between 0 and 1 return: requested "sequence" DP does not support XPath 2.0 sequences. So return <r>num1</r><r>num2</r>... --> <func:function name= "dp:random-sequence"> <xsl:param name= "numberOfItems"/> <xsl:variable name= "bytes" select= "dp:random-bytes(4*$numberOfItems)"/> <func:result select= "dp:random-sequenceR($bytes)" /> </func:function> <!-- input: $str, HTML data from http: //home.ubalt.edu/ntsbarsh/Business-stat/otherapplets/Randomness.htm $values, random values between 0 and 1 to insert into the INPUT fields of $str --> <func:function name= "dp:replaceData"> <xsl:param name= "str"/> <xsl:param name= "values"/> <xsl:choose> <xsl:when test= "count($values/*) = 0"> <func:result select= "$str"/> </xsl:when> <xsl:otherwise> <xsl:variable name= "last" select= "count($values/*)"/> <xsl:variable name= "rest"> <xsl:copy-of select= "$values/*[position() &lt; $last]"/> </xsl:variable> <xsl:variable name= "from" select= "concat('&lt;INPUT name=P',$last,' ([^v]*)value=&quot;([0-9.]*)&quot;[ ]*>')"/> <xsl:variable name= "to" select= "concat('&lt;INPUT name=P',$last,' $1value=&quot;',$values/*[$last],'&quot;>')"/> <xsl:variable name= "repl" select= "regexp:replace($str, $from, '', $to)"/> <func:result select= "dp:replaceData($repl,$rest)"/> </xsl:otherwise> </xsl:choose> </func:function> <!-- type= "String" dp:input- and dp:output-mapping for dealing with HTML data. --> <dp:input-mapping href= "String.ffd" type= "ffd"/> <dp:output-mapping href= "String.ffd" type= "ffd"/> <!-- Set Content-Type of response to text/html. Fill 20 INPUT fields with random values. --> <xsl:template match= "/Conversion/string"> <dp:set-http-response-header name= "'Content-Type'" value= "'text/html'"/> <dp:freeze-headers/> <Conversion> <string> <xsl:value-of select= "dp:replaceData(.,dp:random-sequence(20))"/> </string> </Conversion> </xsl:template> </xsl:stylesheet> </pre>

    Hermann.
    String.ffd:
    <?xml version="1.0" encoding="UTF-8" ?>
    <File version="2.1" name="Conversion">
        <Field name="string" type="String" />
    </File>
    

    Attachments

    Updated on 2014-03-25T03:36:13Z at 2014-03-25T03:36:13Z by iron-man
  • HermannSW
    HermannSW
    6065 Posts

    Re: Random Number Generator?

    ‏2011-01-09T00:56:30Z  
    • HermannSW
    • ‏2011-01-09T00:51:13Z
    String.ffd:
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr"><?xml version="1.0" encoding="UTF-8" ?> <File version="2.1" name="Conversion"> <Field name="string" type="String" /> </File> </pre>
    Screenshot of sample run of above mentioned MPGW service.
  • XIntegration50
    XIntegration50
    97 Posts

    Re: Random Number Generator?

    ‏2014-11-12T06:29:07Z  
    • HermannSW
    • ‏2011-01-09T00:56:30Z
    Screenshot of sample run of above mentioned MPGW service.

    Hi Herman,

    Can I have the export of  MPGW ?

    Thanks,

    Venkat

  • HermannSW
    HermannSW
    6065 Posts

    Re: Random Number Generator?

    ‏2014-11-12T10:54:09Z  

    Hi Herman,

    Can I have the export of  MPGW ?

    Thanks,

    Venkat

    Hi,

    I did build the export based on stylesheet attached above.

    For some reason only the 1st request goes to backend and retrieved the HTML page, which is filled with random values then. Please just use different URLs in browser like "http://yourBox:7798/x" and "http://yourBox:7798/y". For each (GET) request with a new URL you will get back filled out HTML form and you can press CALCULATE button to determine random number quality.

    Just to make this clear,  dp:random-btyes()  always returns new random values, its just that response rule does not get any input on 2nd backend call and therefore returns nothing to browser, which just lets old browser page unchanged.

    Because above listing was done before forum defacing SW update, here it is again:

    <xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:dp="http://www.datapower.com/extensions" 
      xmlns:regexp="http://exslt.org/regular-expressions"
      xmlns:func="http://exslt.org/functions"
      extension-element-prefixes="dp" 
    >
    
    <!--
         input:  base64 encoded 4 byte sequence
                 (eg. by "dp:random-bytes(4)")
         return: value between 0 and 1 represented by input
    -->
      <func:function name="dp:scale4">
        <xsl:param name="bytes4"/>
    
        <func:result 
          select="dp:radix-convert($bytes4,64,10) div 4294967296"
        />
      </func:function>
    
    <!--
         Recursively create sequence of random values
    -->
      <func:function name="dp:random-sequenceR">
        <xsl:param name="bytes"/>
    
    <xsl:variable name="junk">
      <xsl:message dp:priority="error">bytes(<xsl:value-of select="$bytes"/>)</xsl:message>
    </xsl:variable>
    
        <func:result>
          <xsl:if test="$bytes != ''">
            <r><xsl:value-of select="dp:scale4(dp:substring-base64($bytes,1,4))"/></r>
    
            <xsl:copy-of select="dp:random-sequenceR(dp:substring-base64($bytes,5,string-length($bytes)))"/>
          </xsl:if>
        </func:result>
      </func:function>
    
    <!--
         input:  number of requested random values between 0 and 1
         return: requested "sequence" 
                 DP does not support XPath 2.0 sequences.
                 So return <r>num1</r><r>num2</r>...
    -->
      <func:function name="dp:random-sequence">
        <xsl:param name="numberOfItems"/>
    
        <xsl:variable name="bytes" 
          select="dp:random-bytes(4*$numberOfItems)"/>
    
        <func:result 
          select="dp:random-sequenceR($bytes)"
        />
      </func:function>
    
    
    
    <!--
         input: 
           $str, HTML data from 
             http://home.ubalt.edu/ntsbarsh/Business-stat/otherapplets/Randomness.htm
           $values, random values between 0 and 1
             to insert into the INPUT fields of $str
    -->
      <func:function name="dp:replaceData">
        <xsl:param name="str"/>
        <xsl:param name="values"/>
    
        <xsl:choose>
          <xsl:when test="count($values/*) = 0">
            <func:result select="$str"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:variable name="last" select="count($values/*)"/>
    
            <xsl:variable name="rest">
              <xsl:copy-of select="$values/*[position() &lt; $last]"/>
            </xsl:variable>
            
            <xsl:variable name="from"
               select="concat('&lt;INPUT name=P',$last,' ([^v]*)value=&quot;([0-9.]*)&quot;[ ]*>')"/>
    
            <xsl:variable name="to"
               select="concat('&lt;INPUT name=P',$last,' $1value=&quot;',$values/*[$last],'&quot;>')"/>
    
            <xsl:variable name="repl" 
              select="regexp:replace($str, $from, '', $to)"/>
    
            <func:result select="dp:replaceData($repl,$rest)"/>
          </xsl:otherwise>
        </xsl:choose>
      </func:function>
    
    <!--
         type="String" dp:input- and dp:output-mapping for 
         dealing with HTML data.
    -->
      <dp:input-mapping href="String.ffd" type="ffd"/>
      <dp:output-mapping href="String.ffd" type="ffd"/>
    
    <!--
         Set Content-Type of response to text/html.
         Fill 20 INPUT fields with random values.
    -->
      <xsl:template match="/Conversion/string">
    
        <dp:set-http-response-header 
          name="'Content-Type'" value="'text/html'"/>
        <dp:freeze-headers/>
    
        <Conversion>
          <string>
            <xsl:value-of select="dp:replaceData(.,dp:random-sequence(20))"/>
          </string>
        </Conversion>
      </xsl:template>
    
    </xsl:stylesheet>
    

     

    Hermann.

    Attachments

  • 6QMG_Elayaraja_Kathirvel
    82 Posts

    Re: Random Number Generator?

    ‏2014-11-20T20:55:22Z  
    • HermannSW
    • ‏2014-11-12T10:54:09Z

    Hi,

    I did build the export based on stylesheet attached above.

    For some reason only the 1st request goes to backend and retrieved the HTML page, which is filled with random values then. Please just use different URLs in browser like "http://yourBox:7798/x" and "http://yourBox:7798/y". For each (GET) request with a new URL you will get back filled out HTML form and you can press CALCULATE button to determine random number quality.

    Just to make this clear,  dp:random-btyes()  always returns new random values, its just that response rule does not get any input on 2nd backend call and therefore returns nothing to browser, which just lets old browser page unchanged.

    Because above listing was done before forum defacing SW update, here it is again:

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr"><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:func="http://exslt.org/functions" extension-element-prefixes="dp" > <!-- input: base64 encoded 4 byte sequence (eg. by "dp:random-bytes(4)") return: value between 0 and 1 represented by input --> <func:function name="dp:scale4"> <xsl:param name="bytes4"/> <func:result select="dp:radix-convert($bytes4,64,10) div 4294967296" /> </func:function> <!-- Recursively create sequence of random values --> <func:function name="dp:random-sequenceR"> <xsl:param name="bytes"/> <xsl:variable name="junk"> <xsl:message dp:priority="error">bytes(<xsl:value-of select="$bytes"/>)</xsl:message> </xsl:variable> <func:result> <xsl:if test="$bytes != ''"> <r><xsl:value-of select="dp:scale4(dp:substring-base64($bytes,1,4))"/></r> <xsl:copy-of select="dp:random-sequenceR(dp:substring-base64($bytes,5,string-length($bytes)))"/> </xsl:if> </func:result> </func:function> <!-- input: number of requested random values between 0 and 1 return: requested "sequence" DP does not support XPath 2.0 sequences. So return <r>num1</r><r>num2</r>... --> <func:function name="dp:random-sequence"> <xsl:param name="numberOfItems"/> <xsl:variable name="bytes" select="dp:random-bytes(4*$numberOfItems)"/> <func:result select="dp:random-sequenceR($bytes)" /> </func:function> <!-- input: $str, HTML data from http://home.ubalt.edu/ntsbarsh/Business-stat/otherapplets/Randomness.htm $values, random values between 0 and 1 to insert into the INPUT fields of $str --> <func:function name="dp:replaceData"> <xsl:param name="str"/> <xsl:param name="values"/> <xsl:choose> <xsl:when test="count($values/*) = 0"> <func:result select="$str"/> </xsl:when> <xsl:otherwise> <xsl:variable name="last" select="count($values/*)"/> <xsl:variable name="rest"> <xsl:copy-of select="$values/*[position() &lt; $last]"/> </xsl:variable> <xsl:variable name="from" select="concat('&lt;INPUT name=P',$last,' ([^v]*)value=&quot;([0-9.]*)&quot;[ ]*>')"/> <xsl:variable name="to" select="concat('&lt;INPUT name=P',$last,' $1value=&quot;',$values/*[$last],'&quot;>')"/> <xsl:variable name="repl" select="regexp:replace($str, $from, '', $to)"/> <func:result select="dp:replaceData($repl,$rest)"/> </xsl:otherwise> </xsl:choose> </func:function> <!-- type="String" dp:input- and dp:output-mapping for dealing with HTML data. --> <dp:input-mapping href="String.ffd" type="ffd"/> <dp:output-mapping href="String.ffd" type="ffd"/> <!-- Set Content-Type of response to text/html. Fill 20 INPUT fields with random values. --> <xsl:template match="/Conversion/string"> <dp:set-http-response-header name="'Content-Type'" value="'text/html'"/> <dp:freeze-headers/> <Conversion> <string> <xsl:value-of select="dp:replaceData(.,dp:random-sequence(20))"/> </string> </Conversion> </xsl:template> </xsl:stylesheet> </pre>

     

    Hermann.

    How about this below

    <xsl:variable name="uuid" select="string(dp:generate-uuid())" />

    then you can concat with your system name with the above uuid?