LinuxQuestions.org
Help answer threads with 0 replies.
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-05-2018, 04:41 AM   #16
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 601

Rep: Reputation: 280Reputation: 280Reputation: 280

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.
Old 01-05-2018, 05:14 AM   #17
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 601

Rep: Reputation: 280Reputation: 280Reputation: 280
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
}
 
Old 01-05-2018, 08:40 AM   #18
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.3 MATE
Posts: 3,772
Blog Entries: 2

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by ntubski View Post
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.
 
Old 01-05-2018, 12:26 PM   #19
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Solus / Slackware 14.2
Posts: 4,908

Rep: Reputation: 933Reputation: 933Reputation: 933Reputation: 933Reputation: 933Reputation: 933Reputation: 933Reputation: 933
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 12:41 PM.
 
Old 01-05-2018, 03:18 PM   #20
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 9 Stable (Stretch) x64
Posts: 2,163

Original Poster
Rep: Reputation: 290Reputation: 290Reputation: 290
Thumbs up

Quote:
Originally Posted by hydrurga View Post
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!
 
Old 01-05-2018, 03:19 PM   #21
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 9 Stable (Stretch) x64
Posts: 2,163

Original Poster
Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by MadeInGermany View Post
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!
 
  


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 11:41 AM
Generate random numbers in C program ssaslam Linux - Newbie 5 10-23-2008 09:28 PM
Generate random numbers in C program ssaslam Linux - Newbie 1 02-22-2008 12:39 AM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 02:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:11 AM.

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