Random numbers generation with awk in BASH shell

前端 未结 3 803
死守一世寂寞
死守一世寂寞 2020-12-03 09:21

I wish to shuffle the lines (the rows) of a file at random then print out to different five files.

But I keep having exactly the same order of lines appeared in file

相关标签:
3条回答
  • 2020-12-03 09:34

    Awk's pseudo-random is not very random, you need to keep seeding, you should be able to use microseconds for most situations, otherwise you may want to look into Bash ${RANDOM} or hitting /dev/urandom direct:

    awk 'BEGIN{"date +%N"|getline rseed;srand(rseed);close("date +%N");print rand()}'

    for((i=1;i<=5;i++));do awk 'BEGIN{"date +%N"|getline rseed;srand(rseed);close("date +%N");print rand()}';done
    
    0 讨论(0)
  • 2020-12-03 09:47

    If you don't provide a seed to srand, it will either use the current date and time or a fixed starting seed (this may vary with the implementation). That means, for the former, if your processes run fast enough, they'll all use the same seed and generate the same sequence.

    And, for the latter, it won't matter how long you wait, you'll get the same sequence each time you run.

    You can get around either of these by using a different seed, provided by the shell.

    awk -v seed=$RANDOM 'BEGIN{srand(seed);}{print rand()" "$0}' ...
    

    The number provided by $RANDOM changes in each iteration so each run of the awk program gets a different seed.

    You can see this in action in the following transcript:

    pax> for i in $(seq 1 5) ; do
    ...> awk 'BEGIN{srand();print rand()}'
    ...> done
    0.0435039
    0.0435039
    0.0435039
    0.0435039
    0.0435039
    
    pax> for i in $(seq 1 5) ; do
    ...> awk -v seed=$RANDOM 'BEGIN{srand(seed);print rand()}'
    ...> done
    0.283898
    0.0895895
    0.841535
    0.249817
    0.398753
    
    0 讨论(0)
  • 2020-12-03 10:00
    #!/bin/bash
    for i in {1..5}
    do
        shuf -o "file$i.txt" shuffling.txt
    done
    
    0 讨论(0)
提交回复
热议问题