Latest LQ Deal: Linux Power User Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] Looking for code to generate 5 random numbers between 1 and 50 and also 2 between 1 and 12.
 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

 01-05-2018, 03:41 AM #16 MadeInGermany Member   Registered: Dec 2011 Location: Simplicity Posts: 899 Rep: Sorry, I overlooked the word "distinct" in post#1. The associative arrays in bash 4 provide an elegant way to find duplicates Code: ```#requires bash 4 getrandom(){ min=\$1 max=\$2 digits=\${3:-1} scale=\$(( max - min + 1 )) sep="" declare -A D for (( i=1; i<=digits; i++ )) do # skip repeated values, repeat until not found in D until d=\$(( RANDOM % scale + min )) [[ -z \${D[\$d]+_} ]] do : done printf "\${sep}%d" "\$d" sep="," # store value (as key) in D D[\$d]= done } r1=\$( getrandom 1 50 5 ) r2=\$( getrandom 1 12 2 ) echo "\$r1;\$r2"``` 2 members found this post helpful.
 01-05-2018, 04:14 AM #17 MadeInGermany Member   Registered: Dec 2011 Location: Simplicity Posts: 899 Rep: And this works with bash>=3 Code: ```getrandom(){ min=\$1 max=\$2 digits=\${3:-1} scale=\$(( max - min + 1 )) sep="" D="," for (( i=1; i<=digits; i++ )) do # skip repeated values, repeat until not found in D until d=\$(( RANDOM % scale + min )) [[ "\$D" != *",\$d,"* ]] do : done printf "\${sep}%d" "\$d" sep="," # store value in D D="\$D\$d," done }```
01-05-2018, 07:40 AM   #18
hydrurga
LQ Guru

Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19 MATE
Posts: 6,100
Blog Entries: 2

Rep:
Quote:
 Originally Posted by ntubski Speaking as one of the thumb-uppers, I marked it as helpful without actually testing it initially (mainly because using shuf is obviously such a better, simpler, and easier solution for the OP's problem than anything else proposed further on in the thread). Only when I later tried to double check that it wouldn't give repeat numbers I discovered that the command didn't actually work as written.
What I can't understand is that I tested it at the time but still managed to write it down incorrectly - I normally would have cut and pasted to avoid such problems. Definitely a problem existing between the chair and the keyboard! Anyway, all's well that ends well.

 01-05-2018, 11:26 AM #19 BW-userx LQ Guru   Registered: Sep 2013 Location: MID-SOUTH USA Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot Posts: 6,945 Rep: you'd need to write an algorithm to check for (1) matching numbers in the set of numbers, if true, recall rand and hope it does not retrun a same number in the set (for some other number) because you're going to have to repeat that check for matching numbers in set to make sure it does not get another matching number. it's almost like chasing your tail hoping to catch it. edit: this gets you that, but it is a fixed set of x = amount of numbers. Code: ```userx@void:~ \$ x=12345 userx@void:~ \$ for((i=5;i>0;i--));do > ((r=RANDOM%i+1)) > echo \${x:r-1:1} > x=\${x:0:r-1}\${x:r} > done 5 4 2 1 3``` not random numbers where you're picking out of a larger pool of numbers to get a random non repeating set of a smaller pool of numbers. edit2 I just scroooled up and seen what might already be done, done. goody.. saves me time in figuring it out Last edited by BW-userx; 01-05-2018 at 11:41 AM.
01-05-2018, 02:18 PM   #20
linustalman
Senior Member

Registered: Mar 2010
Location: Ireland
Distribution: Debian 9 Stable (Stretch) x64
Posts: 2,923

Original Poster
Rep:

Quote:
 Originally Posted by hydrurga Are you going to buy the person who writes the code a bottle of champagne when you win the lottery with those numbers? Check out the shuf command here: https://www.eduonix.com/blog/shell-s...ell-scripting/ Code: ```shuf -i 1-50 -n 5 shuf -i 1-12 -n 2```
Hi hydrurga. That also works fine. Kudos!

01-05-2018, 02:19 PM   #21
linustalman
Senior Member

Registered: Mar 2010
Location: Ireland
Distribution: Debian 9 Stable (Stretch) x64
Posts: 2,923

Original Poster
Rep:
Quote:
 Originally Posted by MadeInGermany Sorry, I overlooked the word "distinct" in post#1. The associative arrays in bash 4 provide an elegant way to find duplicates Code: ```#requires bash 4 getrandom(){ min=\$1 max=\$2 digits=\${3:-1} scale=\$(( max - min + 1 )) sep="" declare -A D for (( i=1; i<=digits; i++ )) do # skip repeated values, repeat until not found in D until d=\$(( RANDOM % scale + min )) [[ -z \${D[\$d]+_} ]] do : done printf "\${sep}%d" "\$d" sep="," # store value (as key) in D D[\$d]= done } r1=\$( getrandom 1 50 5 ) r2=\$( getrandom 1 12 2 ) echo "\$r1;\$r2"```
Excellent work again MIG. Kudos!

 Tags euro, generate, lotto, numbers, random

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post linustalman Programming 73 05-03-2017 10:41 AM ssaslam Linux - Newbie 5 10-23-2008 08:28 PM ssaslam Linux - Newbie 1 02-21-2008 11:39 PM guguma Programming 4 04-02-2007 01:42 PM

LinuxQuestions.org

All times are GMT -5. The time now is 12:48 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -