LinuxQuestions.org
Visit Jeremy's Blog.
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, 02:03 PM   #16
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 hydrurga View Post
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.
The excellent solution offered by pan64 in post #7 satisfies the no-repeats requirement without a loop and without the accept/reject logic.

Daniel B. Martin
 
1 members found this post helpful.
Old 04-25-2017, 02:09 PM   #17
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 danielbmartin View Post
The excellent solution offered by pan64 in post #7 satisfies the no-repeats requirement without a loop and without the accept/reject logic.

Daniel B. Martin
Ah, my apologies. Behind the curve as usual.
 
Old 04-25-2017, 02:21 PM   #18
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 hydrurga View Post
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.
exactly

if it matches roll again check again or just check it then slap in your own number yourself using hard code, then check that number against all of the others to be sure that was not used yet, if yes, then try again.
 
Old 04-25-2017, 03:00 PM   #19
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by danielbmartin View Post
The excellent solution offered by pan64 in post #7 satisfies the no-repeats requirement without a loop and without the accept/reject logic.
As does the original excellent solution offered by danielbmartin in post #5!

Either is so excellent that differentiating between them is mostly just splitting hairs!

I have not used seq or shuf for so long they would not have come readily to mind!
 
1 members found this post helpful.
Old 04-25-2017, 03:24 PM   #20
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 astrogeek View Post
As does the original excellent solution offered by danielbmartin in post #5!

Either is so excellent that differentiating between them is mostly just splitting hairs!

I have not used seq or shuf for so long they would not have come readily to mind!
I didn;t even loook at that one when I wrote mine and does not use that much nor a pipes
just a simple shuffle
Code:
userx%slackwhere ⚡ ~ ⚡> shuf -i1-47 -n6
gets 6 random within the limit 1-47 numbers. so whats so thumbs up on #5 post?

five function calls to get the same?
1. seq 47 2.| 3. shuf 2.| 4. head -6 2. | 5. tr

seq 47|shuf|head -6|tr

totaling 7 because pipe was called more than once.

Plus another call to print it out to the screen

spruce it up a bit to print on one line instead of 6


the 7 calls 8 adding the extra call to have it print to screen.
Code:
SixRandom=$(seq 47|shuf|head -6|tr "\n" " ")
echo "SixRandom =" $SixRandom
3 function calls and a print to screen all in one line.
Code:
echo $(shuf -i1-47 -n6 | tr "\n" " ")
11 7 42 17 19 34
userx%slackwhere ⚡
ONE liner - done.
but who's counting?
just saying.

Last edited by BW-userx; 04-25-2017 at 03:45 PM.
 
1 members found this post helpful.
Old 04-25-2017, 03:49 PM   #21
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,357
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by linustalman View Post
I want to generate a line of 6 numbers with no repeating numbers.
The shuf option back in #6 ought to do that with no repetition.
 
Old 04-25-2017, 03:49 PM   #22
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by BW-userx View Post
Code:
userx%slackwhere ⚡ ~ ⚡> shuf -i1-47 -n6
gets 6 random within the limit 1-47 numbers. so whats so thumbs up on #5 post?
I did not intend to slight yours, which is another very good use of shuf.

But it came after #5 and (by continuity of thought) #7, both of which were clearly stated and met the original criteria as I understood it, that the output should be a single line and non-repeating.

Quote:
Originally Posted by linustalman View Post
Something like: echo $(( $RANDOM %47 + 1))

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

Quote:
Originally Posted by linustalman View Post
I want to generate a line of 6 numbers with no repeating numbers.
My intent was simply to acknowledge danielbmartin's solution in kind, as he had graciously acknowledged pan64's. It isn't always and only about the code.

Last edited by astrogeek; 04-25-2017 at 03:55 PM. Reason: Completed thought
 
Old 04-25-2017, 04:00 PM   #23
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 astrogeek View Post
I did not intend to slight yours, which is another very good use of shuf.

But it came after #5 and (by continuity of thought) #7, both of which were clearly stated and met the original criteria as I understood it, that the output should be a single line and non-repeating.
the only continuing of thought was formatting the output as stated I did not even look at his before posting mine without the tr as well as one needs to take into consideration now many function calls needs to be preformed to accomplish the same task as most programmers not ALL go by.

less lines of code and function calls = faster execution time

that is not me that just is.

I am not really diss'ing that other one I am just stating the facts.

again I didn't even look at his in the first place go and look at my first post on shuf with out the tr I just made it print on one line instead the second time after looking to see what all the ho ra was about in post #5

Quote:
Originally Posted by astrogeek View Post
Which was later confirmed...



My intent was simply to acknowledge danielbmartin's solution in kind, as he had graciously acknowledged pan64's. It isn't always and only about the code.
yes it is always about the code in programming. as stated above.

think about it.

deploying a safety bag one does not what a lot of code to have to be processed before that bag pops out to save someone. less lines of code to get executed = faster deliver time in deployment of safety bag to ones face to stop it from going through the windshield.


oh and btw I am not bucking for points for my ego - just saying.

Last edited by BW-userx; 04-25-2017 at 04:13 PM.
 
Old 04-25-2017, 04:15 PM   #24
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Quote:
Originally Posted by linustalman View Post
I want to generate a line of 6 numbers with no repeating numbers.
I'd take numbers 1-47, shuffle them, and then take 6.

In Python 2:

Code:
import random
numbers = range(1, 48)
random.shuffle(numbers)
print numbers[:6]
And I see that different implementations of this have already been posted.

Last edited by dugan; 04-25-2017 at 04:24 PM.
 
Old 04-25-2017, 04:25 PM   #25
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 dugan View Post
I'd take numbers 1-47, shuffle them, and then take 6.

In Python 2:

Code:
import random
numbers = range(1, 48)
random.shuffle(numbers)
print numbers[:6]
And I see that different implementations of this half already been posted.
it is ok by me. he never stated what language he wanted it in. it is good to see some one thinking outside the box like python in how it does things like this.
Me, I do not play with snakes.
 
Old 04-25-2017, 04:48 PM   #26
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=BW-userx;5702073]
Code:
userx%slackwhere ⚡ ~ ⚡> shuf -i1-47 -n6
This solution wins the prize for being concise. Thank you, BW-userx, for teaching us the full power of shuf.

pan64 and Laserbeak suggested the time command for measuring and comparing execution times. Using time to measure several solutions (1000 executions each) they are found to be almost equal in system time. That was unexpected.

The numbers vary slightly from test to test, but this is a typical result:
Code:
Method #4 of LQ Member danielbmartin.

real	0m2.284s
user	0m0.060s
sys	0m0.072s

Method of LQ Guru pan64.

real	0m1.238s
user	0m0.124s
sys	0m0.064s

Method of LQ Senior Member BW-userx.

real	0m0.648s
user	0m0.040s
sys	0m0.060s
Daniel B. Martin

Last edited by danielbmartin; 04-25-2017 at 04:57 PM.
 
2 members found this post helpful.
Old 04-25-2017, 04:58 PM   #27
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=danielbmartin;5702115]
Quote:
Originally Posted by BW-userx View Post
Code:
userx%slackwhere ⚡ ~ ⚡> shuf -i1-47 -n6
This solution wins the prize for being concise. Thank you, BW-userx, for teaching us the full power of shuf.

pan64 and Laserbeak suggested the time command for measuring and comparing execution times. Using time to measure several solutions (1000 executions each) they are found to be almost equal in system time. That was unexpected.

The numbers vary slightly from test to test, but this is a typical result:
Code:
Method #4 of LQ Member danielbmartin.

real	0m2.284s
user	0m0.060s
sys	0m0.072s

Method of LQ Guru pan64.

real	0m1.238s
user	0m0.124s
sys	0m0.064s

Method of LQ Senior Member BW-userx.

real	0m0.648s
user	0m0.040s
sys	0m0.060s
Daniel B. Martin
you're welcome.

yea I win hehe

Last edited by BW-userx; 04-25-2017 at 05:02 PM.
 
Old 04-25-2017, 05:06 PM   #28
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
[QUOTE=danielbmartin;5702115]
Quote:
Originally Posted by BW-userx View Post
Code:
userx%slackwhere ⚡ ~ ⚡> shuf -i1-47 -n6
This solution wins the prize for being concise. Thank you, BW-userx, for teaching us the full power of shuf.

pan64 and Laserbeak suggested the time command for measuring and comparing execution times. Using time to measure several solutions (1000 executions each) they are found to be almost equal in system time. That was unexpected.

The numbers vary slightly from test to test, but this is a typical result:
Code:
Method #4 of LQ Member danielbmartin.

real	0m2.284s
user	0m0.060s
sys	0m0.072s

Method of LQ Guru pan64.

real	0m1.238s
user	0m0.124s
sys	0m0.064s

Method of LQ Senior Member BW-userx.

real	0m0.648s
user	0m0.040s
sys	0m0.060s
Daniel B. Martin
"user" is the time it spends actually running your code. "sys" is the time it spends as root doing system tasks like i/o called by your program. "real" is the real amount of time the program takes from launch to exit, but as said before, that can be affected by other programs running on your computer as it multitasks. So you should really add user and sys together to get the real time spent by the processor(s) on your program.

Last edited by Laserbeak; 04-25-2017 at 05:07 PM.
 
1 members found this post helpful.
Old 04-25-2017, 05:23 PM   #29
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 Laserbeak View Post
... you should really add user and sys together to get the real time spent by the processor(s) on your program.
Thank you for this explanation. Forming these sums confirms the superiority of the concise solution posted by BW-userx.

Could/should the time command be enhanced to cite the sum?

Daniel B. Martin
 
Old 04-25-2017, 06:23 PM   #30
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
Thank you for this explanation. Forming these sums confirms the superiority of the concise solution posted by BW-userx.

Could/should the time command be enhanced to cite the sum?

Daniel B. Martin
No problem.

As it is the output of time really isn't standardized anyway. Some systems output something like:
Code:
0.000u 0.000s 0:00.17 0.0%      0+0k 0+0io 0pf+0w
(courtesy Wikipedia)

So I doubt it's going to be changed and standardized anytime soon.

You could write a wrapper around it to do this yourself though...
 
  


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 03:41 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