LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2017, 05:52 AM   #1
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,711

Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Question 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.
 
Old 04-25-2017, 06:01 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Presumably you've already thought of wrapping that in a loop that cycles six times. Can you say more about your requirements?
 
1 members found this post helpful.
Old 04-25-2017, 07:19 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,923
Blog Entries: 44

Rep: Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158
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.
 
Old 04-25-2017, 07:28 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,474

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
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))
 
Old 04-25-2017, 11:08 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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

Last edited by danielbmartin; 04-25-2017 at 11:09 AM.
 
5 members found this post helpful.
Old 04-25-2017, 11:36 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by linustalman View Post
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]}" 
}

Last edited by BW-userx; 04-25-2017 at 12:01 PM.
 
2 members found this post helpful.
Old 04-25-2017, 11:41 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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.
 
3 members found this post helpful.
Old 04-25-2017, 12:04 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by pan64 View Post
... 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
 
Old 04-25-2017, 12:14 PM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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.
 
1 members found this post helpful.
Old 04-25-2017, 12:49 PM   #10
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by danielbmartin View Post
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
________________________________________________________________________________
 
1 members found this post helpful.
Old 04-25-2017, 12:52 PM   #11
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by pan64 View Post
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
________________________________________________________________________________
| =>

Last edited by Laserbeak; 04-25-2017 at 01:01 PM.
 
Old 04-25-2017, 01:10 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

Last edited by BW-userx; 04-25-2017 at 01:12 PM.
 
Old 04-25-2017, 01:48 PM   #13
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,711

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Smile

Quote:
Originally Posted by Turbocapitalist View Post
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.
 
Old 04-25-2017, 01:51 PM   #14
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 04-25-2017, 01:56 PM   #15
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by linustalman View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed - replacing random numbers followed with space and more random chars gluposti Linux - Newbie 7 05-07-2012 07:36 PM
Generate random numbers in C program ssaslam Linux - Newbie 5 10-23-2008 08:28 PM
Generate random numbers in C program ssaslam Linux - Newbie 1 02-21-2008 11:39 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration