Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230
I'm developing a version of the game Mastermind in bash script. It results to be that when I try to make my non repetitive random code, sometimes, the process takes much more time that the expected.
for i in {1..5}
do
if [ $i -eq 1 ]
then
random_num="$(( $RANDOM % 8 + 1 ))"
code=$random_num
else
until [ $full_list -eq 4 ]
do
random_num="$(( $RANDOM % 8 + 1 ))"
for element in $code
do
if [ $element -ne $random_num ]
then
(( full_list++ ))
else
full_list=0
break
fi
done
if [ $full_list -ne 0 ]
then
code="$code $random_num"
fi
done
fi
done
For instance I presume that it is just the "while" which came across with kind of an infinite repeated numbers. OR I did something wrong and can't see it right now.
So in case is the "while"....
Is there a way to limit it? So in case it takes to long to generate the number, starts the loop over?
Does bash allow recursive functions??
Thanks a lot for your time and support!!
Last edited by Ignition on 2019-01-08 15:41, edited 1 time in total.
(Preface:I have initialized full_list with 0 before I analysed your snippet.)
Your problem is that you don't reset full_list and get therefore sometimes in an endless loop. See this example:
full_list: 0
current compare: 8 3
current code: 8
full_list: 1
current compare: 8 4
current compare: 3 4
current code: 8 3
full_list: 3
current compare: 8 6
current compare: 3 6
current compare: 4 6
current code: 8 3 4
full_list: 6
current compare: 8 5
current compare: 3 5
current compare: 4 5
current compare: 6 5
current code: 8 3 4 6
full_list: 10
current compare: 8 6
current compare: 3 6
current compare: 4 6
current code: 8 3 4 6 5
full_list: 0
current compare: 8 2
current compare: 3 2
current compare: 4 2
current compare: 6 2
current compare: 5 2
current code: 8 3 4 6 5
full_list: 5
current compare: 8 1
current compare: 3 1
current compare: 4 1
current compare: 6 1
current compare: 5 1
current compare: 2 1
current code: 8 3 4 6 5 2
full_list: 11
current compare: 8 6
current compare: 3 6
current compare: 4 6
current code: 8 3 4 6 5 2 1
full_list:0
You will never be able to reach full_list equal 4 again to leave the loop. The only situation your snippet works, is when the fifth number is already in $code and resets full_list for you. See: