LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Looking for code to generate 6 random numbers between 1 and 47. (https://www.linuxquestions.org/questions/programming-9/looking-for-code-to-generate-6-random-numbers-between-1-and-47-a-4175604559/)

linustalman 04-25-2017 05:52 AM

Looking for code to generate 6 random numbers between 1 and 47.
 
Hi.

I'm looking for code to generate 6 random numbers between 1 and 47.

Something like: echo $(( $RANDOM %47 + 1))

but instead of spitting out 1 number at a time - it outputs 6 at once.

Thanks.

Turbocapitalist 04-25-2017 06:01 AM

Presumably you've already thought of wrapping that in a loop that cycles six times. Can you say more about your requirements?

onebuck 04-25-2017 07:19 AM

Moderator response
 
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

TenTenths 04-25-2017 07:28 AM

Code:

echo $(( $RANDOM %47 + 1));echo $(( $RANDOM %47 + 1));echo $(( $RANDOM %47 + 1));echo $(( $RANDOM %47 + 1));echo $(( $RANDOM %47 + 1));echo $(( $RANDOM %47 + 1))

danielbmartin 04-25-2017 11:08 AM

This is one way to do it ...

This code ...
Code:

SixRandom=$(seq 47|shuf|head -6|tr "\n" " ")
echo "SixRandom =" $SixRandom

... produced this result ...
Code:

SixRandom = 13 10 35 34 37 9
This code delivers six different random numbers. The solution offered by TenTenths in post #4 delivers six random numbers with a possibility of duplicates.

Which is "right?" That depends on your application and the degree of mathematical rigor required.

Daniel B. Martin

BW-userx 04-25-2017 11:36 AM

Quote:

Originally Posted by linustalman (Post 5701841)
Hi.

I'm looking for code to generate 6 random numbers between 1 and 47.

Something like: echo $(( $RANDOM %47 + 1))

but instead of spitting out 1 number at a time - it outputs 6 at once.

Thanks.

you forgot one important thing.
in what language?

loop me
Code:


#!/bin/bash

for (( i = 0 ; i < 6 ; i++ ))
{
  echo "$((RANDOM%46+1))"
}

shuf
Code:

userx%slackwhere ⚡ ~ ⚡> shuf -i1-47 -n6
16
23
33
19
22
31

array
Code:

for (( i = 0 ; i < 6 ; i++ ))
{
    arrayme[i]="$((RANDOM%46+1))"
}

for (( i = 0 ; i < 6 ; i++ ))
{
    echo "${arrayme[i]}"
}


pan64 04-25-2017 11:41 AM

I don't really like to serve full solution, but here is a much faster version of post #5:
PHP Code:

SixRandom=( $( echo -{1..47}\\n|shuf ) )
echo 
"SixRandom = " ${SixRandom[@]:1:6

post #4 is perfect too, and you can also put that echo into a for loop to make that line shorter.

danielbmartin 04-25-2017 12:04 PM

Quote:

Originally Posted by pan64 (Post 5701980)
... here is a much faster version of post #5:
Code:

SixRandom=( $( echo -e {1..47}\\n|shuf ) )
echo "SixRandom = " ${SixRandom[@]:1:6}


I have only a hazy notion of how to compare execution times of different solutions. Please post your method for doing this. I coded three solutions but posted only one. I'd like to measure all three to see which is fastest. Fast is good!

Daniel B. Martin

pan64 04-25-2017 12:14 PM

you need to know if it was really faster (just because it invokes less processes).
Otherwise there is a command named time to measure and also you may repeat that script in a loop 10000000 times to make that measurement more reliable.
By the way it is really hard to make exact comparison, because it is a multitasking environment running several hundred processes in the same time.
you can also try to use strace -f to check how many processes were forked/generated.

Laserbeak 04-25-2017 12:49 PM

Quote:

Originally Posted by danielbmartin (Post 5701988)
I have only a hazy notion of how to compare execution times of different solutions. Please post your method for doing this. I coded three solutions but posted only one. I'd like to measure all three to see which is fastest. Fast is good!

Daniel B. Martin

Use the time command.

Code:


| =>time ls

<contents of my directory>

real        0m0.010s
user        0m0.003s
sys        0m0.004s

| => time vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free:                              10878.
Pages active:                          1634541.
Pages inactive:                        1628863.
Pages speculative:                          618.
Pages throttled:                              0.
Pages wired down:                        784734.
Pages purgeable:                          34898.
"Translation faults":                870076644.
Pages copy-on-write:                  29506462.
Pages zero filled:                    449674383.
Pages reactivated:                      4756779.
Pages purged:                          1291145.
File-backed pages:                      718744.
Anonymous pages:                        2545278.
Pages stored in compressor:            1590080.
Pages occupied by compressor:            132629.
Decompressions:                        6895352.
Compressions:                          10868635.
Pageins:                              15217684.
Pageouts:                                95295.
Swapins:                                1023442.
Swapouts:                              1467680.

real        0m0.008s
user        0m0.002s
sys        0m0.002s
________________________________________________________________________________


Laserbeak 04-25-2017 12:52 PM

Quote:

Originally Posted by pan64 (Post 5701992)
you may repeat that script in a loop 10000000 times to make that measurement more reliable.

Yes, this is a good idea. As you can see from my results, the time for just one simple command is very short. I tried it, and 10,000 times seems good.

Code:

| => cat lsloop.bash
#!/bin/bash

for i in `seq 1 10000`;
do
  ls > /dev/null 2>&1
done
________________________________________________________________________________
| => time ./lsloop.bash

real        0m32.653s
user        0m16.230s
sys        0m10.797s
________________________________________________________________________________
| =>


BW-userx 04-25-2017 01:10 PM

doesn't this "how fast is your code being completed" question need to have how fast is your computer equated into it too?

code evaluation = count of conditions being tested + time to test every one of them + cpu speeds - cpu load at time of execution.
Quote:

less conditions + slower cpu = total time cpu load = 0
more conditions + faster cpu = same total time? cpu load = 0

linustalman 04-25-2017 01:48 PM

Quote:

Originally Posted by Turbocapitalist (Post 5701844)
Presumably you've already thought of wrapping that in a loop that cycles six times. Can you say more about your requirements?

Hi TC.

I want to generate a line of 6 numbers with no repeating numbers.

Laserbeak 04-25-2017 01:51 PM

Quote:

Originally Posted by BW-userx (Post 5702022)
doesn't this "how fast is your code being completed" question need to have how fast is your computer equated into it too?

code evaluation = count of conditions being tested + time to test every one of them + cpu speeds - cpu load at time of execution.

Ignore real time, that depends on CPU load, but the user and sys times are the time actually used for this program, it doesn't include anything else.

hydrurga 04-25-2017 01:56 PM

Quote:

Originally Posted by linustalman (Post 5702034)
Hi TC.

I want to generate a line of 6 numbers with no repeating numbers.

Do you work for the Irish Lottery? ;-)

Imo, you're just going to have to store each number as it's generated and compare newly-generated numbers against those already generated in order to determine whether or not to accept or reject the newly-generated number. Loop until 6 discrete values have been obtained.


All times are GMT -5. The time now is 02:55 PM.