Random numbers generation with awk in BASH shell

前端 未结 3 823
死守一世寂寞
死守一世寂寞 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
    

提交回复
热议问题