LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Random (https://www.linuxquestions.org/questions/programming-9/random-4175680424/)

 vmill 08-14-2020 06:29 PM

Random

Not sure if programming forum is best but I am trying a simple script on Fedora for a random number generator. I know it is not truly random and am ok with a cheesy hack.

I searched and found "generate random number in specific range" examples:
generating a random number between 11 and 30: echo \$[ \$RANDOM % 20 + 11 ]

It seems to work, but where does 20 come into anything? In other examples I found, the first number is the high end of the range. If I wanted something random between 10 & 15 what would I use?

Confused

 dogpatch 08-14-2020 06:48 PM

\$RANDOM % 20 generates a random number betwwen 0 and 19, add 11 to get a number from 11 to 30

For a value between 10 & 15, use \$[\$RANDOM % 6 + 10]

 dugan 08-14-2020 06:53 PM

Quote:
 Originally Posted by dogpatch (Post 6155968) For a value between 10 & 15, use \$[\$RANDOM % 6 + 10]
Which, I'll add, is:

Code:

`echo \$[RANDOM % ((15 - 10 + 1)) + 10]`
That should make it completely clear.

 vmill 08-14-2020 07:39 PM

Quote:
 Originally Posted by dogpatch (Post 6155968) \$RANDOM % 20 generates a random number betwwen 0 and 19, add 11 to get a number from 11 to 30 For a value between 10 & 15, use \$[\$RANDOM % 6 + 10]
I found a couple of pages that basically said the same thing (the example) and the man file, and not explaining how it was actually formed. I understand it now.

Thanks for the explanation.

 michaelk 08-14-2020 07:44 PM

Quote:
 \$RANDOM % X + Y
To elaborate on the previous posts basically \$RANDOM outputs a signed 16 bit integer i.e a number between 0-32767

To output a smaller range you use the modulo function. The modulo function returns the remainder of an integer division operation. You would take the modulo using the value of the desired range then add the offset.

Quote:
 \$RANDOM % 6 + 1
As another example to output a random number from 1-6 you use 6 for the modulo which outputs 0-5 then add 1. A simpler function is shuf which does all the range calculations for you. For the same example

Quote:
 shuf -i 1-6 -n 1
Will output a random number between 1-6 and display 1 number instead the default which is 20. See man pages for shuf for more details.

I see that you have figured it out...

 AnanthaP 08-15-2020 06:09 AM

% IS THE MODULO. ie % 20 returns integers between 0 and 19. Adding 11 to it, you get integers between 11 and 30.

 ondoho 08-15-2020 07:01 AM

I was surprised to see the construct \$[ ... ] in:
Code:

`echo \$[ \$RANDOM % 20 + 11 ]`
Since when is this supported? Is it a bash thing? Does it replace \$(( ... )) only, or also other (non-arithmetic) constructs?

 GazL 08-15-2020 12:40 PM

\$[ ] is an obsolete syntax used in early bash versions, that was superseded by \$(( )).

 ondoho 08-16-2020 03:07 AM

^ Thank you!

 Fat_Elvis 09-01-2020 12:51 AM

You can read directly from /dev/random (/dev/urandom if you need many bytes).
Code:

```read -n1 RAND < /dev/urandom printf "%q\n" \${RAND}```

 All times are GMT -5. The time now is 02:55 AM.