[SOLVED] Looking for code to generate 6 random numbers between 1 and 47.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
... 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.
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.
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.
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
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"
}
}
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.