0

I have this script that extracts 200 random characters from a set:

tail -n+2 file.fasta | tr -d '\n' > newfile n=$(stat -c "%s" newfile) r=$(shuf -i1-"$((n-200+1))" -n1) newfile tail -c+"$r" | head -c200

Do anyone knows if it is possible to change shuf for rand() using a seed of time (srand(time(0))? I tried to change my script without any success...

Any suggestions? thanks in advance

GSQ
  • 37
  • 5
  • `--random-source=file` might interest you. https://gnu.org/software/coreutils/shuf – Panki Dec 10 '20 at 16:06
  • Hi @Panki, I was actually looking for change shuf to srand(time(0)) but I did not had any success. Do you know how I could do that in order to maintain the funcionality of my script? – GSQ Dec 10 '20 at 18:11
  • There are no `time(0)` or `srand()` shell commands so it's had to understand what you're really asking about. – Ed Morton Dec 10 '20 at 18:14
  • It's unclear what you mean by `srand(time(0))`. If that is C code, then it just sets the initial seed for the random number generator. It's not something that `shuf` could be changed out for. It's unclear what your asking about. – Kusalananda Dec 10 '20 at 18:25
  • Hi @Kusalananda ! I wanted to add a seed of time using srand(time(0)) to make my script the most random possible. Yes it is a C code so my idea was to replace shuf for rand() using this seed but so far I had no success. Any help? My problem is that I cant manage to change my script propetly. – GSQ Dec 10 '20 at 18:43
  • shuf does self-seed from some random entropy sample, and you are unlikely to do better using just time. In particular, if you are running your sampling in a loop, using current time in libc only gives you whole seconds, so several iterations could seed identically. – Paul_Pedant Dec 11 '20 at 09:42
  • Thank you @Paul_Pedant, that was ver helpfull – GSQ Dec 11 '20 at 09:49

1 Answers1

2

There is a Bash variable called RANDOM. Every time you read it, its value is a different random integer between 0 and 32767 (inclusive). Not a wide range, but it may suffice.

You can seed the random sequence by assigning a number to RANDOM. I typically use the shell's own pid for this.

Paul--) RANDOM=$$
Paul--) for j in {1..6}; do printf ' %d' $RANDOM; done
 16928 18765 4814 6954 3017 31155
Paul--) 

Not clear why the shuf is unacceptable. It provides the scaling (which you would otherwise need to do with shell arithmetic), it has a huge range (up to 2^63 - 1), and it executes in a couple of milliseconds. (It can be slow to shuffle a file because it reads the whole file before it picks the lines, but with -i it works a whole lot smarter.)

If you explain what further requirement you have, we might be able to provide a better solution.

Paul_Pedant
  • 8,228
  • 2
  • 18
  • 26