1

My existing script can yield "blue,blue,red,red" but don't want any color to be repeated.

#!/bin/sh

c1="white"
c2='yellow'
c3='orange'
c4='red'
c5='green'
c6='blue'

array=( "$c1" "$c2" "$c3" "$c4" "$c5" "$c6"  )

rand0=$RANDOM
rand0=$[ $rand0 % 6 ]

rand1=$RANDOM
rand1=$[ $rand1 % 6 ]

rand2=$RANDOM
rand2=$[ $rand2 % 6 ]

rand3=$RANDOM
rand3=$[ $rand3 % 6 ]

r0=${array[$rand0]}
r1=${array[$rand1]}
r2=${array[$rand2]}
r3=${array[$rand3]}

mastermind=( "$r0" "$r1" "$r2" "$r3" )

How can I filter for duplicates and re-roll them to avoid duplicates?

11
  • 1
    So repeat choosing a color at each step until you pick a new, unique non-repeated color. With your example - repeat choosing a number form 0-5 until you pick a number that wasn't already picked. Or a better solution - don't pick a number from 0-5, pick a number from a set/list/array of numbers, from which you remove the numbers/elements already chosen. Commented Nov 28, 2019 at 21:28
  • i really did not get it Commented Nov 28, 2019 at 23:20
  • Ok. Let's pretend you are rolling a dice to choose a non-repeating number from 1 to 6. How would you choose two non-repeating numbers? You roll a dice the first time, let's say you get a number, let's pretend it's 3. Then you roll the second time, and let's assume you are out of luck - it's 3 again. What would you do then? Commented Nov 28, 2019 at 23:23
  • repeat giving the second random until i get a number != 3 Commented Nov 29, 2019 at 1:02
  • $[...] is extremely obsolete (to the point that it hasn't been documented for decades; where did you learn about it?). Use $((...)) instead. Commented Nov 29, 2019 at 2:39

1 Answer 1

2

One approach will be to remove items that have been selected from 'array', and check that new selection do not referenced removed items. It attempt to generalize the behavior of the OP code to - it loops over the number of requested items (4), and it allows for arbitrary number of items in the input array, without code modification.

#! /bin/bash

array=( white yellow orange red green blue )
mastermind=()
  # Count entries in array
N=${#array[@]}
for i in {0..3} ; do

   # Find R, where array[R] is valid
   while R=$((RANDOM%N)) ; do [ "${array[R]}" ] && break ; done

   # Add to result
   mastermind+=( ${array[R]} )

   # Mark item R as invalid
   unset 'array[R]'
done
echo "${mastermind[@]}"
Sign up to request clarification or add additional context in comments.

2 Comments

but bro this is bash. i need it on shell. help me please it been 2 weeks now and i could not find how to do it
@TounssiSimo question is tagged 'bash', and partial solution that you posted are using bash features (arrays, ...). Which shell do you need (ksh, dash, ash, ...). Usually 'sh' is symlinked to one of them. If bash is your default shell, change the first line to /bin/sh :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.