-2

For example, if I have the following command:

python generate.py --outdir=out --trunc=1 --seeds=128,8755,90,543468

I want to use bash to generate random numbers and place them in the seeds argument, and have the seeds be between specific interval (e.g., 1-8000). Like this:

rand_seed_gen= im_a_func_that_generates_4_random_seeds # pseudo code
python generate.py --outdir=out --trunc=1 --seeds=rand_seed_gen

EDIT: Also, is there a way to multiply the number of random integers without explicitly passing each one individually? For example (again, pseudo code): ${RANDOM} * 10 to return a comma separated list of 10 random integers.

dedede
  • 197
  • 11
  • Does this answer your question? [How to generate random number in Bash?](https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash) – Ulrich Eckhardt Mar 14 '21 at 18:17
  • @dedede : Please don't ask two questions in a single post. Create two different posts for this. – user1934428 Mar 15 '21 at 08:14

1 Answers1

1

You could do this

python generate.py --outdir=out --trunc=1 --seeds=${RANDOM},${RANDOM},${RANDOM}

From the bash manpage

RANDOM

Each time this parameter is referenced, it expands to a random integer between 0 and 32767. Assigning a value to this variable seeds the random number generator. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.

But if you need to set a range, what you can do is

# Generate random numbers, replacing nullbytes with commas
randvars=$(shuf -z -i 1-8000 -n 4 | tr '\0' ',')
# Delete the last comma
randvars=${randvars::-1}

python generate.py --outdir=out --trunc=1 --seeds=${randvars}

Where -i 1-8000 is the range and -n 4 is the amount you want to return.

644
  • 629
  • 4
  • 14
  • Also, is there a way to multiply the number of random integers without explicitly passing each one individually? For example (again, pseudo code): `${RANDOM} * 10` to return a comma separated list of 10 random integers. – dedede Mar 14 '21 at 18:11
  • @dedede Edited the post to include a way to set range – 644 Mar 14 '21 at 18:16
  • it didn't work when I did it line by line but it worked when I inserted the whole thing into the argument like this `python generate.py --outdir=out --trunc=1 --seeds=\`randvars=$(shuf -z -i 1-8000 -n 4 | tr '\0' ','); randvars=${randvars::-1}; echo ${randvars}\` – dedede Mar 14 '21 at 18:20
  • @dedede You can unquote the `--seeds="${randvars}"` part, so do `--seeds=${randvars}` – 644 Mar 14 '21 at 18:24