Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- declare -a name Rl S R
- n=0 s=0
- while read LINE; do
- n=$(( n + 1 ))
- name[$n]="$LINE"
- done
- while [ $s -lt $n ]; do # For each participant as a sender
- s=$(( s + 1 )) x=0 y=0 Rl=( )
- while [ $x -lt $n ]; do
- x=$(( x + 1 ))
- [ $x -ne $s ] || continue
- [ "${S[$x]}" ] && continue # Recipient already has sender
- y=$(( y + 1 ))
- Rl[$y]=$x
- done
- if [ $y -gt 0 ]; then
- R[$s]=${Rl[$((((RANDOM<<15)|RANDOM)%y+1))]}
- else # Randomization left sender sending to self; swap senders
- R[$s]=${R[1]} R[1]=$s
- fi
- [ $s -eq 1 ] || echo "${name[$s]} -> ${name[${R[$s]}]}"
- S[${R[$s]}]=$s
- done
- [ $n -lt 1 ] || echo "${name[1]} -> ${name[${R[1]}]}"
Add Comment
Please, Sign In to add comment