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

 

 

 

[SOLVED] Non repetitive random number

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Ignition
Posts: 11
Joined: 2018-12-07 12:42

[SOLVED] Non repetitive random number

#1 Post by Ignition »

Hello everybody!! :D

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.

Code: Select all

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!! :mrgreen: :mrgreen:
Last edited by Ignition on 2019-01-08 15:41, edited 1 time in total.

cronoik
Posts: 310
Joined: 2015-05-20 21:17

Re: Non repetitive random number

#2 Post by cronoik »

(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:

Code: Select all

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:

Code: Select all

full_list: 0
current compare:  4 7
current code:  4
full_list: 1
current compare:  4 8
current compare:  7 8
current code:  4 7
full_list: 3
current compare:  4 8
current compare:  7 8
current code:  4 7 8
full_list: 0
current compare:  4 8
current compare:  7 8
current code:  4 7 8
full_list: 0
current compare:  4 3
current compare:  7 3
current compare:  8 3
current code:  4 7 8
full_list: 3
current compare:  4 8
current compare:  7 8
current code:  4 7 8 3
full_list: 0
current compare:  4 7
current code:  4 7 8 3
full_list: 0
current compare:  4 3
current compare:  7 3
current compare:  8 3
current code:  4 7 8 3
full_list: 0
current code:  4 7 8 3
full_list: 0
current compare:  4 2
current compare:  7 2
current compare:  8 2
current compare:  3 2
current code:  4 7 8 3
When the fifth number doesn't reset $full_list and is added to $code, then you end up an endless loop. Potential fix:

Code: Select all

...
until [ $full_list -eq 4 ]
                     do
                     full_list=1
                     random_num="$(( $RANDOM % 8 + 1 ))"
...
Have a nice day!

Ignition
Posts: 11
Joined: 2018-12-07 12:42

Re: Non repetitive random number

#3 Post by Ignition »

Thank you very much!!!!! That worked perfectly!

I was so blind that didn't have into account that scenario :?
cronoik wrote:(Preface:I have initialized full_list with 0 before I analysed your snippet.)
Yeah.. Sorry, I have it initialized at the beginning of the function. Forgot to add it to the posted code. :oops:

I really appreciate the help and time invested :D

Post Reply