[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.
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.
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!
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.
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
________________________________________________________________________________
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
________________________________________________________________________________
| =>
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.