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 04-25-2017, 08:45 PM   #31
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187

If I may suggest ... "don't bother to 'over-think this thing,' cuz it 'probably' doesn't matter."

If you drew six random numbers out of a "hat" that contained the numbers [1..47], then the odds of duplication would be 1/47 ... a very-acceptably small number, to be sure.

Should you discover that a duplication did occur, you could simply replace the entry with another random number, then repeat the duplicate-detection process, knowing that the odds of finding a duplicate will not have changed.

It is actually possible to mathematically demonstrate that, in this case, it is more advantageous to "deal with duplicates, should they actually occur," than to construct an alternative process in which duplicates could not possibly occur.

However: "Six numbers? Out of 47?" WHOGAS=Who Gives A ..." NIKE="Just Do It™."

Last edited by sundialsvcs; 04-25-2017 at 08:47 PM.
 
Old 04-25-2017, 08:52 PM   #32
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by sundialsvcs View Post
If I may suggest ... "don't bother to 'over-think this thing,' cuz it 'probably' doesn't matter."
While I totally agree some programmers over-think simple problems to make them as efficient as possible, when, especially today, processors are so fast it really doesn't matter. But, in this case I just think your solution is unacceptably inelegant compared to others proposed that guarantee no duplicates on the first draw.

Sorry.
 
Old 04-25-2017, 10:01 PM   #33
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,590

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
Quote:
Originally Posted by Laserbeak View Post
So you should really add user and sys together to get the real time spent by the processor(s) on your program.
This would not give credit to parallel programs though.

Quote:
Originally Posted by sundialsvcs View Post
If I may suggest ... "don't bother to 'over-think this thing,' cuz it 'probably' doesn't matter."
+1, it's silly to optimize a task as trivial as this one.

Quote:
Originally Posted by Laserbeak View Post
guarantee no duplicates on the first draw.
The OP didn't specify whether or not there may be duplicates.

Oops, I missed #13

Last edited by ntubski; 04-26-2017 at 07:16 AM. Reason: OP did actually ask for no dups.
 
Old 04-25-2017, 10:13 PM   #34
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Quote:
Originally Posted by sundialsvcs View Post
... If you drew six random numbers out of a "hat" that contained the numbers [1..47], then the odds of duplication would be 1/47 ...
I respectfully disagree.

If you do "sampling without replacement" the odds of duplication would be zero.
If you do "sampling with replacement" the odds of duplication would be
(1/47)+(2/47)+(3/47)+(4/47)+(5/47) = (15/47).

I took a basic statistics course a long time ago and memory fades. Corrections are welcome.

Daniel B. Martin
 
2 members found this post helpful.
Old 04-25-2017, 10:25 PM   #35
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by ntubski View Post
This would not give credit to parallel programs though.


+1, it's silly to optimize a task as trivial as this one.



The OP didn't specify whether or not there may be duplicates.

Yes time adds the time spent by all processors together. So "real" time can actually be less than "user" + "sys" since multiple processor time is being used concurrently. That wouldn't happen here since it's not a multithreaded app.

Well it eventually came up (no duplicates).

Last edited by Laserbeak; 04-26-2017 at 04:36 AM.
 
Old 04-26-2017, 01:18 AM   #36
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,238

Rep: Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453
first you need to get one random number (1..47), next only (1..46) and add 1 if equal or greater than the first number.
next generate (1..45) and again increment if required...
It is much faster than shuf I think (and do not need to fork at all).

you can use /usr/bin/time instead of the built-in time to get different kind of result. Probably better.

Last edited by pan64; 04-26-2017 at 01:20 AM.
 
Old 04-26-2017, 06:39 AM   #37
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 698

Rep: Reputation: 110Reputation: 110
This question reminded me of some script I once wrote to generate some numbers for playing AD&D and not having any dice at hand.

Code:
if [[ "$1" == "" ]]
then
        printf "$0 #d#+#\n"
        printf "Example: $0 1d8+0\n"
        exit
fi

NUMDICE=$(echo $1 | cut -d 'd' -f 1)
DICESIZE=$(echo $1 | cut -d 'd' -f 2 | cut -d '+' -f 1)
PLUS=$(echo $1 | cut -d '+' -f 2)

if (( $NUMDICE < 1 ))
then
        printf "Amount of dice must be greater than 0\n"
        exit
fi

if ((DICESIZE < 1))
then
        printf "Give a positive integer greater than 0 for the size of the dice"
        exit
fi

if [[ "$PLUS" == "$1" ]]
then
        PLUS=0
fi

printf "Running $NUMDICE dices of type d$DICESIZE adding $PLUS.\n"
TOTAL=0
for((RUN=0;RUN < $NUMDICE;RUN++))
do
        ((ROLL=(RANDOM % DICESIZE)+1)) # 1 .. dicesize
        printf "Rolled a $ROLL\n"
        ((TOTAL+=ROLL))
        printf "Total is now: $TOTAL\n"
done

if (( PLUS > 0))
then
        printf "Adding $PLUS to $TOTAL...\n"
        ((TOTAL+=PLUS))
fi
printf "The total is now: $TOTAL\n"

Last edited by Ramurd; 04-26-2017 at 06:58 AM. Reason: Stupid to set RANDOM to PID
 
Old 04-26-2017, 09:01 AM   #38
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Ramurd View Post
This question reminded me of some script I once wrote to generate some numbers for playing AD&D and not having any dice at hand.
which accounts for duplicates. that is a nice script.

if OP don't want dups then it is a matter of comparison, if match, call function to get a new random number, replace the old with the new and then ________________, before printing the end results to screen.

it is not that complicated

SOMETHING like this perhaps?
Code:
#!/bin/bash
#random 1 - 47 in an array

for (( i = 0 ; i < 6 ; i++ ))
{
	array[i]=$((RANDOM%46+1))
	echo "${array[i]}"
}

for (( i = 0 ; i < ${#array[@]} ; i++ ))
{
   echo "in eye = $i"

	for (( j = 0 ; j < ${#array[@]} ; j++ ))
	{
		if [[ ${array[i]} == ${array[j+1]} ]] ;
		then
		        echo "match"
			echo "${array[i]} == ${array[j+1]}"
			
		else
			echo "no match"
			echo "${array[i]} == ${array[j+1]}"
		fi
			echo "in jay=: $j"
	}
}

Last edited by BW-userx; 04-26-2017 at 11:18 AM.
 
Old 04-26-2017, 01:56 PM   #39
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
Thumbs up

The 2 I went with are:

https://www.linuxquestions.org/quest...9/#post5701973

(shuf one)
https://www.linuxquestions.org/quest...9/#post5701979

Thanks guys.
 
Old 04-26-2017, 01:58 PM   #40
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
Smile

Quote:
Originally Posted by hydrurga View Post
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.
No. :-)

I just play the lotto.
 
Old 04-26-2017, 01:59 PM   #41
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
Quote:
Originally Posted by Turbocapitalist View Post
The shuf option back in #6 ought to do that with no repetition.
Yes, that was perfect also.
 
Old 04-26-2017, 02:00 PM   #42
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
Quote:
Originally Posted by BW-userx View Post
you forgot one important thing. in what language? ...
Hi BW-userx.

I don't mind what language - I'm not a programer. This is to generate random lotto numbers. :-)

BTW - your shuf command was my favorite and I added to an alias command for quick usage.

Last edited by linustalman; 04-26-2017 at 02:01 PM.
 
Old 04-26-2017, 02:02 PM   #43
linustalman
Senior Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 10 (Buster) x64
Posts: 4,504

Original Poster
Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
Quote:
Originally Posted by onebuck View Post
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
Thanks, sorry for posting in General -- I was unsure of the proper place.
 
Old 04-26-2017, 02:13 PM   #44
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by linustalman View Post
Hi BW-userx.

I don't mind what language - I'm not a programer. This is to generate random lotto numbers. :-)

BTW - your shuf command was my favorite and I added to an alias command for quick usage.
OIC
don't forget to close your eyes just before you run it.
 
Old 04-26-2017, 02:14 PM   #45
onebuck
Moderator
 
Registered: Jan 2005
Location: Summer Midwest USA, Central Illinois, Winter Central Florida
Distribution: SlackwareŽ
Posts: 13,654
Blog Entries: 36

Rep: Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844
Moderator response

Quote:
Originally Posted by linustalman View Post
Thanks, sorry for posting in General -- I was unsure of the proper place.
No problem. That's my job here at LQ to insure proper posting to the forums. Only one of many tasks as a moderator. :
 
  


Reply


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] sed - replacing random numbers followed with space and more random chars gluposti Linux - Newbie 7 05-07-2012 07:36 PM
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 07:27 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
Open Source Consulting | Domain Registration