Shuffling lines of a file with a fixed seed?

后端 未结 3 1402
粉色の甜心
粉色の甜心 2020-12-16 14:43

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 am using is as follows:

sort -R file.txt |          


        
3条回答
  •  我在风中等你
    2020-12-16 15:13

    If you're randomly shuffling lines, you're not sorting. I haven't seen a sort with --random-source prompt before. It'd be interesting if it does exist. However, that's not sorting the lines in a fixed order.

    I believe you'll have to write a program to that, and I don't think Bash can quite do it.

    Actually, it might. The $RANDOM environment variable selects a random number from 0 to 32767. You can assign a seed to RANDOM and the random number sequence will appear over and over. You can use a card dealing algorithm. Read in each line into a Bash array, then use the card dealing algorithm to pick each line.

    I'm not going to write a test program -- especially in Bash, but you should get the idea.

提交回复
热议问题