Shuffling the lines of a file with a fixed seed?

I want to shuffle the lines of a file with a fixed seed so that I always get the same random order. The command I use is as follows:

sort -R file.txt | head -200 > file.sff

what change can i make it sort with fixed random sowing?

Ted

+5
source share
3 answers

The GNU implementation sorthas an argument --random-source. Passing this argument with a file name with known contents will result in a reliable set of results.

See the Random Sources documentation in the GNU coreutils manual for the following sample implementation and example:

get_seeded_random()
{
  seed="$1"
  openssl enc -aes-256-ctr -pass pass:"$seed" -nosalt \
    </dev/zero 2>/dev/null
}

shuf -i1-100 --random-source=<(get_seeded_random 42)

GNU sort coreutils, :

sort --random-source=<(get_seeded_random 42) -R file.txt | head -200 > file.sff
+5

, sort, . Bash , $RANDOM. , , , :

RANDOM=$$

RANDOM=$(date '+%s')

... , , , - :

$ RANDOM=12345; echo $RANDOM
28207
$ RANDOM=12345; echo $RANDOM
28207

, mapfile:

$ mapfile -t a < source.txt

:

$ for i in ${!a[@]}; do a[$((RANDOM+${#a[@]}))]="${a[$i]}"; unset a[$i]; done

Bash .

, , . - , $RANDOM . , :

... a[$(( (RANDOM<<15)+RANDOM+${#a[@]} ))]= ...

30- int 15- int.

0

, . sort --random-source. , . , .

, , , Bash .

, . $RANDOM 0 32767. RANDOM, . . Bash, , .

, Bash, .

-3

All Articles