-3

The question is related to bash, Linux like Ubuntu, /dev/random and /dev/urandom .

How to generate a random number in a range which can be positive or negative, by /dev/random or /dev/urandom on bash ?

Known are:

Follow are a sample for a not searched solution to do it in a range on bash, by not searched function "RANDOM" and only in a positive range.

min=1
max=1000
rnd_count=$((RANDOM%(max-min+1)+min))
echo $rnd_count

Follow are a sample for a solution to do it by /dev/urandom on not searched language C:

> ##################################################
> # Random number generator, crypto quality
> #################################################    
> # Returns a random floating point number between $min and $max, inclusive
> # With the default arguments, this is almost the same as expr rand()
> # Doesn't work on Windows, only on Unix-based OS such as MacOSX and Linux proc getRandomNumber {{min 0} {max 1}} {    global tcl_platform 
> if {$tcl_platform(platform) == "unix"} {
>         set f [open /dev/urandom rb] ; set eightRandomBytes [read $f 8] ; close $f
>        binary scan $eightRandomBytes h16 eightRandomBytesHex
>        # n is an integer from 0 to 18446744073709551615 inclusive... lossless conversion
>        set n [scan $eightRandomBytesHex %llx]
>        # map n to min-max inclusive... maybe we lose a little randomness here (precision)
>        set randomNumber [expr (($n/18446744073709551615.0) * ($max - $min)) + $min]
>        return $randomNumber    } else {
>        error "getRandomNumber: Only works with Unix-based platforms"    }  }

Source: https://wiki.tcl-lang.org/page/Cryptographically+secure+random+numbers+using+%2Fdev%2Furandom

Follow are a sample for a partly solution by a not searches python solution by a python function:

python -c "import random; print random.randint(1,1000)" 

A again, How to generate a random number in a range which can be positive or negative, by /dev/random or /dev/urandom on bash (not on other languages )?

A partly solution is to generate a random number by bash random or urandom on positive range works on follow way, how to do this on positive and negative range too, is unknown fo me. And its not pure bash. A pure bash solution are wanted.

echo "$(od -An -N4 -tu4 /dev/urandom) % 15 + 1" | bc

or even better

echo "$(od -An -N4 -tu4 /dev/random) % 15 + 1" | bc  
2
  • 1
    Your question is very hard to follow because you already put in code that works in the middle there for bash that uses random numbers. Have you even attempted anything on your own and maybe are stuck? If this is a homework assignment, we are not here to write your code, but we will help you if you get stuck. – Terrance Feb 23 at 15:32
  • 1
    The sample code worked fine for me in terminal. A one liner works perfect: min=1; max=1000; rnd_count=$((RANDOM%($max-$min+1)+$min)); echo $rnd_count and it gives a different number each time. Again, we are not here to teach you how to do it, but we are here to help in a Q&A style if you get stuck on something. – Terrance Feb 23 at 16:10
1

The simplest, cleanest way I can think of is what you already have in your question:

min=-100; max=1; rnd_count=$((RANDOM%($max-$min+1)+$min)); echo $rnd_count

You can easily convert that into a function:

getRand(){
    min="${1:-1}"   ## min is the first parameter, or 1 if no parameter is given           
    max="${2:-100}" ## max is the second parameter, or 100 if no parameter is given
    rnd_count=$((RANDOM%($max-$min+1)+$min)); ## get a random value using /dev/urandom
    echo "$rnd_count"
}

If you either paste those lines into your terminal or add them to your ~/.bashrc file and open a new terminal, you can now do:

$ getRand -5 5
-3

That gives a random number between -5 and 5.

4
  • @Eddy763 if you want true random values, for use in encryption or anything else sensitive, then you don't work in bash. If you just want a more or less random value, then this is fine. For more complex things you need more complex tools. – terdon Feb 23 at 18:02
  • @ terdon, now I addet a oartly solution which works on bash by randonm and urandom on a positive range, a still not on apositive and negaive range. – Eddy763 Feb 23 at 21:22
  • @Eddy763 I don't understand. Neither od nor bc are bash. So that isn't a bash solution at all. Didn't you say you wanted bash? Are you open to other tools? – terdon Feb 23 at 23:08
  • Now I delete some remarks, becorse se question are closed depend on to much remarks. Pls. delete some remarks too. – Eddy763 Feb 24 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.