LinuxQuestions.org
Review your favorite Linux distribution.
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 01-02-2018, 04:02 PM   #1
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 9 Stable (Stretch) x64
Posts: 2,646

Rep: Reputation: 336Reputation: 336Reputation: 336Reputation: 336
Question Looking for code to generate 5 random numbers between 1 and 50 and also 2 between 1 and 12.


Hi.

I want code to randomly pick 5 distinct numbers between 1 and 50 inclusive and also randomly pick 2 distinct numbers between 1 and 12 inclusive.

So an example output would be: 1,2,3,4,5; 9,8

Thanks.
 
Old 01-02-2018, 04:12 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19 MATE
Posts: 5,346
Blog Entries: 2

Rep: Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712
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

Last edited by hydrurga; 01-04-2018 at 07:54 PM. Reason: Oops!
 
4 members found this post helpful.
Old 01-02-2018, 04:21 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,236

Rep: Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345
Python:

Code:
import random
l1 = range(1, 51)
random.shuffle(l1)
print l1[:5]
l2 = range(1, 13)
random.suffle(l2)
print l2[:2]
 
Old 01-03-2018, 10:35 AM   #4
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 794

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
With bash builtins:
Code:
getrandom(){
  min=$1; max=$2; digits=${3:-1}
  scale=$(( max - min + 1 ))
  sep=""
  for ((i=1; i<=digits; i++))
  do
    printf "${sep}%d" $(( RANDOM % scale + min ))
    sep=","
  done
}
r1=$( getrandom 1 50 5 )
r2=$( getrandom 1 12 2 )
echo "$r1;$r2"
 
2 members found this post helpful.
Old 01-03-2018, 11:11 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,398

Rep: Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238
c++ just call it with your limits in the prams
Code:
size_t getRany(int min, int max)
{
   std::random_device seed;
   std::mt19937 gen(seed());
   std::uniform_int_distribution<int> dist(min, max);
   
return ( dist(gen) );
}
 
1 members found this post helpful.
Old 01-03-2018, 01:59 PM   #6
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 9 Stable (Stretch) x64
Posts: 2,646

Original Poster
Rep: Reputation: 336Reputation: 336Reputation: 336Reputation: 336
Thumbs up

Quote:
Originally Posted by MadeInGermany View Post
With bash builtins:
Code:
getrandom(){
  min=$1; max=$2; digits=${3:-1}
  scale=$(( max - min + 1 ))
  sep=""
  for ((i=1; i<=digits; i++))
  do
    printf "${sep}%d" $(( RANDOM % scale + min ))
    sep=","
  done
}
r1=$( getrandom 1 50 5 )
r2=$( getrandom 1 12 2 )
echo "$r1;$r2"
Perfect! Thank you MIG.
 
Old 01-04-2018, 02:45 PM   #7
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 9 Stable (Stretch) x64
Posts: 2,646

Original Poster
Rep: Reputation: 336Reputation: 336Reputation: 336Reputation: 336
Exclamation

I spoke too soon.

After using the script a few times, I notice that it sometimes repeats the same numbers.
e.g.
31,48,25,27,16;5,5
38,43,2,2,46;5,8
33,7,33,22,27;11,2

Could you also make it output the numbers in ascending order?

Last edited by linustalman; 01-04-2018 at 02:47 PM. Reason: spelling error
 
Old 01-04-2018, 03:59 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,398

Rep: Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238
random is not random and yes, you could just store the numbers in a var array then sort it before printing it out.
 
Old 01-04-2018, 04:12 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,667
Blog Entries: 6

Rep: Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531
Quote:
Originally Posted by linustalman View Post
Hi.

I want code to randomly pick 5 distinct numbers between 1 and 50 inclusive and also randomly pick 2 distinct numbers between 1 and 12 inclusive.

So an example output would be: 1,2,3,4,5; 9,8

Thanks.
Should the two sets of numbers be disjoint? That is, should the set from 1 to 12 never include numbers in the 1 to 50 set?

As already noted, just add them to an array and reject duplicates.

Note also that rejecting duplicates makes each successive choice less random and somewhat dependent on the order in which you select them. For example, if you choose the 1 to 12 set after the 1 to 50 set, and the sets are disjoint, then the the first 1 to 12 choice is actually one of seven worst case, and the second is then one of six. Only you can know if that is important to your use.

Last edited by astrogeek; 01-04-2018 at 04:14 PM.
 
Old 01-04-2018, 04:23 PM   #10
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 9 Stable (Stretch) x64
Posts: 2,646

Original Poster
Rep: Reputation: 336Reputation: 336Reputation: 336Reputation: 336
Quote:
Originally Posted by astrogeek View Post
Should the two sets of numbers be disjoint? That is, should the set from 1 to 12 never include numbers in the 1 to 50 set?

As already noted, just add them to an array and reject duplicates.

Note also that rejecting duplicates makes each successive choice less random and somewhat dependent on the order in which you select them. For example, if you choose the 1 to 12 set after the 1 to 50 set, and the sets are disjoint, then the the first 1 to 12 choice is actually one of seven worst case, and the second is then one of six. Only you can know if that is important to your use.
Hi AG.

It does not matter if the 1-12 set contain the same numbers as the 1-50 set. e.g. this is ok: 2,4,10,30,50;2,4 -- the thing that matters is that neither set repeat numbers in their respective sets.

I do not know how to program. I have tried many times but alas it's not for me.

Last edited by linustalman; 01-04-2018 at 04:25 PM.
 
Old 01-04-2018, 06:27 PM   #11
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 873

Rep: Reputation: 208Reputation: 208Reputation: 208
Just out of interest, I wanted to know these two things:
  1. OP what are your 2075 posts all about?
  2. Why nobody in this thread asked you initially what you tried before posting solutions? Or is that reserved only for certain profiles?
 
Old 01-04-2018, 06:38 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,667
Blog Entries: 6

Rep: Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531
Quote:
Originally Posted by AnanthaP View Post
Just out of interest, I wanted to know these two things:
  1. OP what are your 2075 posts all about?
  2. Why nobody in this thread asked you initially what you tried before posting solutions? Or is that reserved only for certain profiles?
What is the point of the first question?

There is no requirement to ask anyone what they have already done. Although that is a commonly used starting point in many cases, it is up to each member whether and how to respond to any given question or member.

If you think there is something improper about any post please use the report button to bring it to the attention of moderators.

Otherwise, please keep comments on topic.

Last edited by astrogeek; 01-04-2018 at 06:43 PM. Reason: Added on topic
 
Old 01-04-2018, 07:19 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,355

Rep: Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509
Quote:
Originally Posted by linustalman View Post
It does not matter if the 1-12 set contain the same numbers as the 1-50 set. e.g. this is ok: 2,4,10,30,50;2,4 -- the thing that matters is that neither set repeat numbers in their respective sets.
Post #2 already has the correct solution (except that it should be -n5, not n=5).
 
1 members found this post helpful.
Old 01-04-2018, 07:56 PM   #14
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19 MATE
Posts: 5,346
Blog Entries: 2

Rep: Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712Reputation: 1712
Quote:
Originally Posted by ntubski View Post
Post #2 already has the correct solution (except that it should be -n5, not n=5).
Spot on, ntubski. I don't know how I managed to make that mistake (and receive three thumbs-up for my incorrect answer!). Now corrected.
 
Old 01-04-2018, 08:39 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,355

Rep: Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509
Quote:
Originally Posted by hydrurga View Post
I managed to make that mistake (and receive three thumbs-up for my incorrect answer!)
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.
 
  


Reply

Tags
euro, generate, lotto, numbers, random


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Looking for code to generate 6 random numbers between 1 and 47. linustalman Programming 73 05-03-2017 10:41 AM
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 01:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration