LinuxQuestions.org [SOLVED] Looking for code to generate 6 random numbers between 1 and 47.
 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.

04-26-2017, 02:57 PM   #46
hydrurga
LQ Guru

Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep:

Quote:
 Originally Posted by BW-userx OIC don't forget to close your eyes just before you run it.
You should ask for a share if he wins it using your method.

04-26-2017, 04:27 PM   #47
BW-userx
LQ Guru

Registered: Sep 2013
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep:
Quote:
 Originally Posted by hydrurga You should ask for a share if he wins it using your method.
Good point

04-27-2017, 07:27 AM   #48
linustalman
Senior Member

Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep:
Quote:
 Originally Posted by hydrurga You should ask for a share if he wins it using your method.

04-27-2017, 08:14 AM   #49
BW-userx
LQ Guru

Registered: Sep 2013
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep:
Quote:
 Originally Posted by linustalman
LAWS of Probability?

if you use on set of numbers and the odds are say: 3000 to 1
you play that set of numbers, it does NOT hit. So you pick a different set of numbers, the odds still stay the same. 3000 to 1
but what if you keep that same set of numbers and play them again. Will that change the odds to 2999 to 1 because one of them was already used?

in other words if one keep playing the odds using the same set of numbers, are the odds of wining increased due to it has to hit eventually because of the process of elimination?

04-27-2017, 08:18 AM   #50
linustalman
Senior Member

Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep:

Quote:
 Originally Posted by BW-userx LAWS of Probability? if you use on set of numbers and the odds are say: 3000 to 1 you play that set of numbers, it does NOT hit. So you pick a different set of numbers, the odds still stay the same. 3000 to 1 but what if you keep that same set of numbers and play them again. Will that change the odds to 2999 to 1 because one of them was already used? in other words if one keep playing the odds using the same set of numbers, are the odds of wining increased due to it has to hit eventually because of the process of elimination?
I would say that if fresh random numbers were used to play the lotto every week vs using the same randomly picked numbers every week -- the odds of winning the jackpot would be the same.

04-27-2017, 08:21 AM   #51
hydrurga
LQ Guru

Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep:
Quote:
 Originally Posted by BW-userx LAWS of Probability? if you use on set of numbers and the odds are say: 3000 to 1 you play that set of numbers, it does NOT hit. So you pick a different set of numbers, the odds still stay the same. 3000 to 1 but what if you keep that same set of numbers and play them again. Will that change the odds to 2999 to 1 because one of them was already used? in other words if one keep playing the odds using the same set of numbers, are the odds of wining increased due to it has to hit eventually because of the process of elimination?
No, the odds of winning are exactly the same. The probability of certain numbers being chosen in any draw is independent of any previous results.

04-27-2017, 08:29 AM   #52
BW-userx
LQ Guru

Registered: Sep 2013
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep:
Quote:
 Originally Posted by hydrurga No, the odds of winning are exactly the same. The probability of certain numbers being chosen in any draw is independent of any previous results.
that sound so "text book" convincing.

though that was the other side of my thought, I just didn't add it in here. Because the rationalization of "if you play that same number long enough it will hit eventually". can be a true statement.

I do suppose the rationalization falls into that is the same as the pool table theory.
If you hit the ball hard enough, it will eventually fall into the hole (pocket).

Last edited by BW-userx; 04-27-2017 at 08:37 AM.

04-28-2017, 07:42 AM   #53
linustalman
Senior Member

Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep:

Quote:
 Originally Posted by BW-userx ... shuf Code: ```userx%slackwhere ⚡ ~ ⚡> shuf -i1-47 -n6 16 23 33 19 22 31``` ...
Would it be possible to output the numbers on the same line and separated by commas?

04-28-2017, 07:59 AM   #54
hydrurga
LQ Guru

Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep:
Quote:
 Originally Posted by linustalman Would it be possible to output the numbers on the same line and separated by commas?
One alternative:

Code:
`shuf -i1-47 -n6 | sed ':a;N;\$!ba;s/\n/,/g'`
Sorted:

Code:
`shuf -i1-47 -n6 | sort -n | sed ':a;N;\$!ba;s/\n/,/g'`

Last edited by hydrurga; 04-28-2017 at 08:02 AM. Reason: sorted

1 members found this post helpful.
 04-28-2017, 08:04 AM #55 Turbocapitalist LQ Guru Contributing Member   Registered: Apr 2005 Distribution: Linux Mint, Devuan, OpenBSD Posts: 5,513 Blog Entries: 3 Rep: Or a perl variant: Code: `shuf -i 1-47 -n 6 | perl -p -00 -e 's/\n(?!\Z)/,/g'` 1 members found this post helpful.
04-28-2017, 08:09 AM   #56
Ramurd
Member

Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 698

Rep:
Quote:
 Originally Posted by BW-userx that sound so "text book" convincing. though that was the other side of my thought, I just didn't add it in here. Because the rationalization of "if you play that same number long enough it will hit eventually". can be a true statement. I do suppose the rationalization falls into that is the same as the pool table theory. If you hit the ball hard enough, it will eventually fall into the hole (pocket).
I think it all depends on the method how the random numbers are generated.

Given a bowl with 47 balls numbered 1 - 47, there's no chance you'll have a duplicate, unless balls are thrown back in the bowl.
The shuf command resembles this method, and that makes it a nice and quick method.

On the other hand; if each time a number is generated is equal and completely random, there's an increasing chance that there will be duplicates.

The issue with this kind of thing is indeed the duplicates; are they possible, how do they affect the outcome... I've never been one for lotteries, so actually I have no idea what the real question is... I find this initial question intriguing in that aspect, one I often detect at work as well; people think they have one question... until they speak with us... they return with more questions. All because the question has to be exact, because the answer needs to be exact.

04-28-2017, 08:12 AM   #57
linustalman
Senior Member

Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep:
Quote:
 Originally Posted by Turbocapitalist Or a perl variant: Code: `shuf -i 1-47 -n 6 | perl -p -00 -e 's/\n(?!\Z)/,/g'`
And also have them sorted in ascending order?

 04-28-2017, 08:14 AM #58 sundialsvcs LQ Guru   Registered: Feb 2004 Location: SE Tennessee, USA Distribution: Gentoo, LFS Posts: 9,078 Blog Entries: 4 Rep: The odds of a six-digit draw from 47 is (1/47) + (1/46) + (1/45) + (1/44) + (1/43) + (1/42). As each digit is drawn, the size of the remaining pool of numbers decreases by one.
04-28-2017, 08:40 AM   #59
danielbmartin
Senior Member

Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep:
Quote:
 Originally Posted by linustalman Would it be possible to output the numbers on the same line and separated by commas?
This code ...
Code:
```SixRandom=\$(shuf -i1-47 -n6|paste -sd,)
echo "SixRandom =" \$SixRandom```
... produced this result ...
Code:
`SixRandom = 46,29,36,17,10,16`
Daniel B. Martin

2 members found this post helpful.
04-28-2017, 08:47 AM   #60
danielbmartin
Senior Member

Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep:
Quote:
 Originally Posted by linustalman And also have them sorted in ascending order?
This code ...
Code:
```SixRandom=\$(shuf -i1-47 -n6|sort -n|paste -sd,)
echo "SixRandom =" \$SixRandom```
... produced this result ...
Code:
`SixRandom = 5,18,32,36,41,44`
Daniel B. Martin

1 members found this post helpful.

 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 gluposti Linux - Newbie 7 05-07-2012 07:36 PM 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 06:54 PM.

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