Find the answer to your Linux question:
Results 1 to 4 of 4
I'm using this bash script to generate 10 digit numbers: Code: head -c4 /dev/urandom| od -An -tu4 I have 2 issues to deal with. 1. This script often also generates ...
  1. #1
    Just Joined! Jakkfrosted's Avatar
    Join Date
    Sep 2006
    Location
    U.S.
    Posts
    36

    Question Help with number generation

    I'm using this bash script to generate 10 digit numbers:
    Code:
    head -c4 /dev/urandom| od -An -tu4
    I have 2 issues to deal with.

    1. This script often also generates 9 digit numbers.

    2. I would like to generate all number sequences within a specific range without having to enter this script for one number at a time.

    Also a question: If I can generate number sequences within a range will they all be unique (no repeats)?

    Any help is appreciated.

    Script source: Text Processing Commands

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    1.

    r=$(head -n1 /dev/urandom | uuencode -m - | grep -o [0-9] | tail -n 10)
    z=
    for c in $r
    do
    z=$z$c
    done
    echo $z
    Caveat : the number may start with zero !

    2. Test for value z between desired range.

  3. #3
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    1.

    This one is better, no leading zero :

    Code:
    r=0
    while [ $r -lt 999999999 ]
    do
      r=$(head -n1 /dev/urandom | uuencode -m - | tr -cd [0-9] | tail -c10)
    done
    echo $r

  4. #4
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    And this one sets the bounds :

    Code:
    r=0
    lbound=2000000000
    ubound=2300000000
    while [ $r -lt $lbound ] || [ $r -gt $ubound ]
    do
      r=$(head -n1 /dev/urandom | uuencode -m - | tr -cd [0-9] | tail -c10)
    done
    echo $r

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...