bash - PHP or shell: How to randomize a list without back to back repeats? -
i wrote in linux bash shell, if there's better solution in php fine.
i need produce random selection array of 12 elements. i've been doing far:
# display/return my_array that's been randomly selected: # random 0 11: r=$(( $random % 12 )) echo ${my_array[$r]}
each time call made, randomly selects element. however, often, "randomly" selects same element in row several times. how can accomplish in bash shell or php make random selection not repeat of last 1 selected? thanks!
r=$last while [ "$last" = "$r" ] r=$(($random % 12)) done export last=$r
if calling script again , again, suppose script name test.sh
need call . test.sh
instead of ./test.sh
, make script run in current shell. else export
not needed. otherwise creating temp file approach robust way of getting last value.
Comments
Post a Comment