Alex Rivera | Logout

Shuffling lines of a file with a fixed seed?

Asked 2011-05-06T16:47:52.433
30

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 | head -200 > file.sff

What change could I make it so that it sorts with a fixed random seed?

Edit
Report

1 Answer

-7

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.

answered 2011-05-06T17:16:17.437

Your Answer