[SOLVED] Looking for code to generate 6 random numbers between 1 and 47.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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
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
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
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.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.