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. |
Presumably you've already thought of wrapping that in a loop that cycles six times. Can you say more about your requirements?
|
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.
|
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)) |
This is one way to do it ...
This code ... Code:
SixRandom=$(seq 47|shuf|head -6|tr "\n" " ") Code:
SixRandom = 13 10 35 34 37 9 Which is "right?" That depends on your application and the degree of mathematical rigor required. Daniel B. Martin |
Quote:
in what language? loop me Code:
Code:
userx%slackwhere ⚡ ~ ⚡> shuf -i1-47 -n6 Code:
for (( i = 0 ; i < 6 ; i++ )) |
I don't really like to serve full solution, but here is a much faster version of post #5:
PHP Code:
|
Quote:
Daniel B. Martin |
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. |
Quote:
Code:
|
Quote:
Code:
| => cat lsloop.bash |
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:
|
Quote:
I want to generate a line of 6 numbers with no repeating numbers. |
Quote:
|
Quote:
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. |