-2

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/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

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

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, 2021 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, 2021 at 16:10

2 Answers 2

2

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.

2
  • @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, 2021 at 18:02
  • @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, 2021 at 23:08
0

You can do on follow way:

#!/bin/bash

get_rnd_num_eq_dis(){ # max Zahlenbereich 0 - 32767
    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
     srnd="$((0x$(dd if=/dev/urandom of=/dev/stdout bs=4 count=1 status=none | xxd -p)))" # SRANDOM Nachbau

    rnd_count=$(( srnd % ( max - min + 1 ) + min ));
    
    echo "$rnd_count"
}

ug=-1000 # untere Grenze
og=1000 # obere Grenze 

var="$(get_rnd_num_eq_dis $ug $og)" # Call the function
echo "$var" # output

sleep 2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .