'Get random number on command line on MacOs (like shuf)
I need add random number to filename. On Linux I use command like
filename$(shuf -i 10000000-99999999 -n 1)
But on MacOs with this command I recieve error:
sh: shuf: command not found
Here are another solution on MacOs for make it?
Solution 1:[1]
Like this maybe:
dd bs=4 count=1 if=/dev/urandom 2>/dev/null | xxd -p
7bfe4143
Or with od
:
head -c 4 /dev/urandom | od -An -tu4
2465874330
Or with bash
's $RANDOM
:
echo $RANDOM$RANDOM
820227815
Solution 2:[2]
Install coreutils with
brew install coreutils
use gshuf
Solution 3:[3]
Bash $RANDOM is a pretty good solution. Alternatively, you can use "jot", which comes pre-installed on Mac:
$ jot -r 1 10000000 99999999
if you want to generate more than one number, just change the first parameter
$ jot -r 36 10000000 99999999 // Generates 36 Unique Numbers
Solution 4:[4]
There is no need to add supplemental tools via HomeBrew, MacOS comes with Ruby and Python builtin. You can do something like:
/usr/bin/ruby -e 'p rand(1...10000000)'
If you want leading zeros:
/usr/bin/ruby -e 'printf "%07d\n", rand(1...10000000)'
You can append the result to a filename prefix by removing the \n
and concatenating. Here's an example:
fname=foo`/usr/bin/ruby -e 'printf "%07d", rand(1...10000000)'`; echo $fname
Solution 5:[5]
A simple approach could to use the current unix timestamp:
filename$(date '+%s')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Akash |
Solution 3 | Kerem BaydoÄŸan |
Solution 4 | |
Solution 5 | tgallacher |