Home

Results 1 to 9 of 9

Thread: Random number

Hybrid View

  1. #1
    Alex McHugh NNTP User

    Random number

    What is the recommended/fashionable way to generate a random number in DirXML-Script these days?


    I know of several approaches, wondering which is best.

    1. EcmaScript - via Math.random
    2. Java - <token-xpath expression="java.util.Random:new()"/>
    3. Generate-Password against a numeric only password policy?? (not tested, just an idea)


    any other ways ? Is the second option still the simplest?

  2. #2
    ab NNTP User

    Re: Random number

    I would think #2 is probably the fastest, #1 the easiest to find an
    example online, and #3 the one that requires the least knowledge of any
    kind of coding (just lots of clicking). If required, I'd probably go with
    #2 as well, yes.

    With that in mind, I always like to know the "why" behind the request.
    What will it be used for, just in case there are other/better options?

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below...

  3. #3
    Alex McHugh NNTP User

    Re: Random number

    ab wrote:

    > I would think #2 is probably the fastest


    I also think this, but i'm somewhat surprised that this has never been replaced with something less DirXML 1.0 (like a built-in token)

    > #1 the easiest to find an
    > example online,


    There are examples of #1 that ship with IDM - in LIBAJC, no need to go online.

    > #3 the one that requires the least knowledge of any
    > kind of coding (just lots of clicking).


    I don't really like option 3 was just thinking is there a way to do this with a built-in token?

    > If required, I'd probably go with
    > #2 as well, yes.
    >
    > With that in mind, I always like to know the "why" behind the request.
    > What will it be used for, just in case there are other/better options?


    I need to write some load balancing code, which was originally written in a script and I'm re-implementing in a driver.

    Know that I could maybe instead use current time and modulus (as in here https://www.netiq.com/communities/co...-policy-rules/ )

    However that really is not in the spirit of the original code. At best it's pseudo-random as it has a predictable sequence for the input.

  4. #4
    ab NNTP User

    Re: Random number

    I believe what was used in the billion-object eDirectory tree many years
    ago (2000?) was hashing of usernames and then distribution of objects
    based on the hash. For example, values starting with a-f go to server0,
    g-l go to server1, m-s go to server2, and t-z go to server3. Bit of
    predictability (both before and after the fact, which could be nice) plus
    pretty easy to change as more servers are added or some are removed. Just
    a thought...

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below...

  5. #5
    Alex McHugh NNTP User

    Re: Random number

    ab wrote:

    > I believe what was used in the billion-object eDirectory tree many years
    > ago (2000?) was hashing of usernames and then distribution of objects
    > based on the hash.


    The requirement here is to check free disk space on destination servers, generate a list of servers that aren't full and then pick one of these randomly.

    However, the idea with hashing usernames is something I have looked into in other projects.

  6. #6

    Re: Random number

    Alex McHugh wrote:

    > I know of several approaches, wondering which is best.
    >
    > 1. EcmaScript - via Math.random


    This is what I do and what gives you the most flexibility. Here's what I use:

    /**
    * Returns a random number between min and max
    * where the lowest and highest possible values only have
    * half the chance to get returned than all other values
    * due to rounding behaviour
    */
    function randomDecimal(min,max,precision) {
    if (min==undefined) min=0;
    if (max==undefined) max=1;
    if (precision==undefined) precision=10;
    return (Math.random()*(max-min)+min).toFixed(precision);
    }

    If you are looking for an integer, another way would be to use

    return (Math.floor(Math.random()*(max-min+1))+min).toString();

    in the function above (and rename it to "randomInteger") or create a random
    string of digits through something like:

    /**
    * Returns a random string (alphanumeric or from a given character set)
    */
    function randomString(len,charset) {
    if (charset==undefined)
    charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno pqrstuvwxyz0123456789';
    var string='';
    for (var i=0; i<len; i++) {
    var pos=Math.floor(Math.random()*charset.length);
    string+=charset.substr(pos,1);
    }
    return string;
    }

    > 2. Java - <token-xpath expression="java.util.Random:new()"/>


    Maybe fastest, but may require post-processing in DirXML-Script to get the
    number range and precision you actually want and loose overall performance that
    way. Math.random() returns a 15 digit precision number between 0 and 1 - not
    always what you finally need.

    > 3. Generate-Password against a numeric only password policy?? (not tested,
    > just an idea)


    Interesting, but keep that an idea... :-)

  7. #7
    Alex McHugh NNTP User

    Re: Random number

    "Lothar Haeger" <lothar.haeger@is4it.de> wrote:
    > Alex McHugh wrote:
    >
    >> 2. Java - <token-xpath expression="java.util.Random:new()"/>

    >
    > Maybe fastest, but may require post-processing in DirXML-Script to get the
    > number range and precision you actually want and loose overall performance that
    > way. Math.random() returns a 15 digit precision number between 0 and 1 - not
    > always what you finally need.


    I went with this option - did not need more precision than an integer.

    Liked the ECMAScript options, will look at those for next project
    --
    If you find this post helpful and are logged into the web interface, show
    your appreciation and click on the star below...

  8. #8
    Join Date
    Dec 2007
    Location
    Brooklyn, NY
    Posts
    6,213

    Re: Random number

    On 12/11/2014 10:22 AM, Alex McHugh wrote:
    > "Lothar Haeger" <lothar.haeger@is4it.de> wrote:
    >> Alex McHugh wrote:
    >>
    >>> 2. Java - <token-xpath expression="java.util.Random:new()"/>

    >>
    >> Maybe fastest, but may require post-processing in DirXML-Script to get the
    >> number range and precision you actually want and loose overall performance that
    >> way. Math.random() returns a 15 digit precision number between 0 and 1 - not
    >> always what you finally need.

    >
    > I went with this option - did not need more precision than an integer.
    >
    > Liked the ECMAScript options, will look at those for next project


    I really liked the ECMA options. Would you (Lothar) mind updating the
    ECMA function Wiki with them please?

    http://wiki.novell.com/index.php/ECMAScript_Examples

    And any other good ones you have handy...


  9. #9
    joakim ganse NNTP User

    Re: Random number


    I would agree with Aaron on that it depends on what it will be used for
    but also on who will update, read and understand the code.

    I would use option 1 or 3, most of the time I would go with 3 since the
    password policy is so good at getting the range correct.
    Also the password token method allows for administrators to change from
    say 7 to 8 characters without editing the code or even a GCV.


    --
    joakim_ganse
    ------------------------------------------------------------------------
    joakim_ganse's Profile: https://forums.netiq.com/member.php?userid=159
    View this thread: https://forums.netiq.com/showthread.php?t=52398


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •