Sobhit Sharma
Sobhit Sharma

Reputation: 713

If RANDOM only goes up to 32767, how can I generate a 9-digit random number?

How to generate 9 digit random number in shell?

I am trying something like this but it only gave numbers below 32768.

#!/bin/bash
mo=$((RANDOM%999999999))
echo "********Random"$mo

Please help

output should be ********Random453351111

Upvotes: 1

Views: 2567

Answers (7)

GoTrained
GoTrained

Reputation: 303

This expression gives a higher range of numbers.

echo $(($RANDOM*$RANDOM))

It can be combined with a range like this:

echo $(((RANDOM%100000)*(RANDOM%100000)*(RANDOM%100000)))

As the generated number can be larger than 9 digits, it can then be cut to the required limit. This example assigns the output to a variable n and then prints n, or it can be used in another expression as needed.

n=$(echo $(((RANDOM%100000)*(RANDOM%100000)*(RANDOM%100000))) | head -c 9)

echo $n

Upvotes: 0

kshji
kshji

Reputation: 11

Using variable SRANDOM if exist (bash >= 5.1 or khs93) and if not, generating 10 number length number using RANDOM. Full builtin.

rand10()
{
   xrand=""
   for xc in {1..4}
   do
         x=$(printf '%04d' $(( RANDOM % 10000 )) )
         xrand=$xrand${x:0:3}
   done
   echo "${xrand:0:10}"
}

[ "$SRANDOM" = "" ] && SRANDOM=$(rand10)
printf '%09d\n' $(( SRANDOM % 1000000000 ))

Upvotes: 0

MUY Belgium
MUY Belgium

Reputation: 2452

Use perl, as follows :

perl -e print\ rand | cut -c 3-11 

Or

perl -MPOSIX -e 'print floor rand 10**9'

Upvotes: 0

0stone0
0stone0

Reputation: 43972

As a work around, we could just simply ask for 1 random integer, for n times:

rand=''
for i in {1..9}; do
    rand="${rand}$(( $RANDOM % 10 ))"
done

echo $rand

Try it online!


Note [1]: Since RANDOM's upper limit has a final digit of 7, there's a slightly lesser change for the 'generated' number to contain 8 or 9's.

Upvotes: 1

James Brown
James Brown

Reputation: 37404

In Linux with /dev/urandom:

$ rnd=$(tr -cd "[:digit:]" < /dev/urandom | head -c 9) && echo $rnd
463559879

Upvotes: 3

Ivan
Ivan

Reputation: 1455

I think this should make it

shuf -i 99999999-999999999 -n 1

Upvotes: 2

Charles Duffy
Charles Duffy

Reputation: 295403

Because of RANDOM's limited range, it can only be used to retrieve four base-10 digits at a time. Thus, to retrieve 9 digits, you need to call it three times.

If we don't care much about performance (are willing to pay process substitution costs), this may look like:

#!/usr/bin/env bash
get4() {
  local newVal=32768
  while (( newVal > 29999 )); do # avoid bias because of remainder
    newVal=$RANDOM
  done
  printf '%04d' "$((newVal % 10000))"
}

result="$(get4)$(get4)$(get4)"
result=$(( result % 1000000000 ))
printf '%09d\n' "$result"

If we do care about performance, it may instead look like:

#!/usr/bin/env bash
get4() {
  local newVal=32768 outVar=$1
  while (( newVal > 29999 )); do # avoid bias because of remainder
    newVal=$RANDOM
  done
  printf -v "$outVar" '%04d' "$((newVal % 10000))"
}

get4 out1; get4 out2; get4 out3
result="${out1}${out2}${out3}"
result=$(( result % 1000000000 ))
printf '%09d\n' "$result"

Upvotes: 1

Related Questions