Sometimes, we are programming some script in Bash …. and we need (for some reason) to generate some random number.
For that you can program an entire application (or function ...) yes, but ... curiously our system has already done that 😀
In a terminal, type the following and press [enter]:
echo $ RANDOM
... a number will appear, they do the same thing again and another number appears, and so on 🙂
What it does is show you a random number (any) between 0 and 32768 (integer, that is, without a comma).
If you need it to be a random number, but between 0 and ... let's say 100, you can put that limit on it 😀
echo $ (($ RANDOM% 100))
The same, another example ... if you want it to be a number between 0 and 29 it would be:
echo $ (($ RANDOM% 29))
Is it understood not? 😀
If they will use it in a bash script they are doing, to assign the generated value (a random number) to a variable it would be:
VARIABLE = `echo $ (($ RANDOM))`
Well, this is it, I don't know about you ... but I know that it will be useful to me at some point hahaha.
regards
10 comments, leave yours
Well, I've been testing it and it only returns a 4-digit number, how can I make it bigger?
Very interesting tip, thank you.
Returns a number between 0 and 32768, I have not been able to get larger numbers.
and can it generate exadecimals ????
VARIABLE = `echo $ (($ RANDOM))`
That works but is not optimal, for the simple reason that RANDOM is a variable and you can do:
variable = $ RANDOM
and that's it! do not run echo in a terminal aprte (which is what you are doing)
Yes, obviously it can be achieved like this… the only difference is that later, to see the number that the variable took (since the user is not a guesser), it would be necessary to do an echo…. and in the end, what I do here is simply do the echo (so that the user sees what number is taken) from the beginning.
Do I make myself understood? 🙂
Another way to generate a random number, although this time it would be this command:
date "+% N" | cut -c 9
That would give us the date in nanoseconds with 9 digits. If we want a single digit, then you put the "cut -c 9" (the last digit is always more random because it is the smallest of the number). If we want 2 figures then we put the "cut - c 8,9". If we want three figures then "cut -c 7-9" (we start using the hyphen).
The only bad thing about this is if we want to get many random numbers in a row in a short time, because this is a random number based on a date with its time. That is, if we make a for with that command we can see that:
$ for i in `seq 1 1 500`; do date "+% N"; done
...
...
...
308311367
310807595
313273093
315725181
318186139
320671403
323360117
325733353
328335462
330694870
333259893
335858999
338375622
340798446
...
...
...
I think it's clear right? The figures on the left are more similar in a short space of time, of course, and those on the right are more “random”.
mmm…. I liked it, I have a mini script, it is painted to generate random numbers, thanks.
It serves .. and a lot ..
especially if you are programming an interface in bash with password, security, etc, etc, etc haha.
Excellent aprote.
Hey.
First of all, of course, congratulations on this excellent website, which I have been following for a long time.
And secondly, make a small note to this entry:
When limiting is done like this:
echo $ (($ RANDOM% 10))
Actually, what you order the interpreter is that your generated number is always the modulus% (remainder of the division) of the subsequent number, in this example, 10.
Any number divided by 10 will never give as a remainder something greater than the divisor itself.
The problem is that it will not give the same number either, because a division by 0 is not logical for the interpreter.
This means that echo $ (($ RANDOM% 10) will give results between 0 and 9, but never 10.
The solution to this conflict is to add one to your limit, so that the same number falls within the random range.
echo $ (($ RANDOM% 11))
This will give results between 0 and 10.
A greeting.
Hello, I was just building something like this, but I ran into a problem.
I want to make 6 different numbers from 00 to 45 but not repeat them.
echo $(($RANDOM%46)) $(($RANDOM%46)) $(($RANDOM%46)) $(($RANDOM%46)) $(($RANDOM%46)) $(($ RANDOM%46))
EX: 17 33 16 36 45 27