• Shell Script Oddity

    From Alan B@21:1/5 to All on Mon Feb 13 09:31:09 2023
    I've been experimenting with some shell scripting recently. I'm using the following line of code to create 2 unique random numbers in range 1 to 12
    and sort them into ascending numerical order:

    numbers=($(jot -r 2 1 12 | sort -n))

    Occasionally it creates exactly the same numbers. Is there some fundamental error in my code or am I expecting too much of jot?

    --
    Cheers, Alan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alan B@21:1/5 to Alan B on Mon Feb 13 10:03:55 2023
    On 2023-02-13, Alan B <[email protected]d> wrote:
    I've been experimenting with some shell scripting recently. I'm using the following line of code to create 2 unique random numbers in range 1 to 12
    and sort them into ascending numerical order:

    numbers=($(jot -r 2 1 12 | sort -n))

    Occasionally it creates exactly the same numbers. Is there some fundamental error in my code or am I expecting too much of jot?

    Incidentally I've used shuf in Linux and also seen this issue very occasionally:

    numbers=($(shuf -i 1-12 -n2 | sort -n))

    --
    Cheers, Alan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alan B@21:1/5 to Theo on Mon Feb 13 11:09:27 2023
    Theo <[email protected]> wrote:
    Alan B <[email protected]d> wrote:
    I've been experimenting with some shell scripting recently. I'm using the
    following line of code to create 2 unique random numbers in range 1 to 12
    and sort them into ascending numerical order:

    numbers=($(jot -r 2 1 12 | sort -n))

    Occasionally it creates exactly the same numbers. Is there some fundamental >> error in my code or am I expecting too much of jot?

    Isn't that to be expected? It's effectively rolling two 12-sided dice for you. When you do that, sometimes you'll roll a double. Why would you
    expect them to be always different?


    Of course! I’m now working on a different solution using the RANDOM
    function plus a test to see if the number has already been generated using
    a checklist …. Unless you know better ;-)

    --
    Cheers, Alan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Theo@21:1/5 to Alan B on Mon Feb 13 10:46:02 2023
    Alan B <[email protected]d> wrote:
    I've been experimenting with some shell scripting recently. I'm using the following line of code to create 2 unique random numbers in range 1 to 12
    and sort them into ascending numerical order:

    numbers=($(jot -r 2 1 12 | sort -n))

    Occasionally it creates exactly the same numbers. Is there some fundamental error in my code or am I expecting too much of jot?

    Isn't that to be expected? It's effectively rolling two 12-sided dice for
    you. When you do that, sometimes you'll roll a double. Why would you
    expect them to be always different?

    Theo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Theo@21:1/5 to Alan B on Mon Feb 13 13:46:42 2023
    Alan B <[email protected]d> wrote:
    Of course! I’m now working on a different solution using the RANDOM function plus a test to see if the number has already been generated using
    a checklist …. Unless you know better ;-)

    One way would be to make a list of numbers from 1 to 12, put them in a
    random order, and then select the first two. On Linux:

    $ seq 12 | shuf | head -2

    does it, printing one number per line. If you want them on the same line:

    $ seq 12 | shuf | head -2 | paste - -s -d ' '

    Theo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris@21:1/5 to Alan B on Mon Feb 13 15:52:01 2023
    Alan B <[email protected]d> wrote:
    I've been experimenting with some shell scripting recently. I'm using the following line of code to create 2 unique random numbers in range 1 to 12
    and sort them into ascending numerical order:

    numbers=($(jot -r 2 1 12 | sort -n))

    Occasionally it creates exactly the same numbers. Is there some fundamental error in my code or am I expecting too much of jot?

    It's perfectly valid to have two random numbers which are the same. It's
    quite likely when sampling only 12 numbers.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alan B@21:1/5 to Theo on Mon Feb 13 16:27:18 2023
    On 2023-02-13, Theo <[email protected]> wrote:
    Alan B <[email protected]d> wrote:
    Of course! I’m now working on a different solution using the RANDOM
    function plus a test to see if the number has already been generated using >> a checklist …. Unless you know better ;-)

    One way would be to make a list of numbers from 1 to 12, put them in a
    random order, and then select the first two. On Linux:

    $ seq 12 | shuf | head -2

    does it, printing one number per line. If you want them on the same line:

    $ seq 12 | shuf | head -2 | paste - -s -d ' '

    Yes those commands work fine in Linux - many thanks. I'll look at writing
    a version using jot. jot can be installed on some Linux systems using the athena-jot package. I haven't yet checked but shuf may be available for
    macOS via MacPorts or Brew?

    In the meantime I'm working on a rather long winded solution using RANDOM.
    All good experience and keeps the old gray cells active :-)

    --
    Cheers, Alan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Tobin@21:1/5 to [email protected] on Tue Feb 14 10:46:14 2023
    In article <tsd5p7$24kvk$[email protected]>,
    Alan B <[email protected]d> wrote:

    Of course! I’m now working on a different solution using the RANDOM >function plus a test to see if the number has already been generated using
    a checklist. Unless you know better ;-)

    What is it you are trying to achieve?

    -- Richard

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alan B@21:1/5 to Richard Tobin on Tue Feb 14 12:01:54 2023
    Richard Tobin <[email protected]> wrote:
    In article <tsd5p7$24kvk$[email protected]>,
    Alan B <[email protected]d> wrote:

    Of course! I’m now working on a different solution using the RANDOM
    function plus a test to see if the number has already been generated using >> a checklist. Unless you know better ;-)

    What is it you are trying to achieve?

    Not a lot to be honest.

    --
    Cheers, Alan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alan B@21:1/5 to All on Tue Feb 14 12:49:51 2023
    On 13 Feb 2023 at 13:46:42 GMT, "Theo" <[email protected]> wrote:

    Alan B <[email protected]d> wrote:
    Of course! I’m now working on a different solution using the RANDOM
    function plus a test to see if the number has already been generated using >> a checklist …. Unless you know better ;-)

    One way would be to make a list of numbers from 1 to 12, put them in a
    random order, and then select the first two. On Linux:

    $ seq 12 | shuf | head -2

    does it, printing one number per line. If you want them on the same line:

    $ seq 12 | shuf | head -2 | paste - -s -d ' '

    This also seems to work including macOS:

    $ seq 12 | sort -R | head -2

    --
    Cheers, Alan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)