Why do I not see the full expected range of random numbers? [duplicate] - bash

This question already has answers here:
Random number from a range in a Bash Script
(19 answers)
Closed 8 years ago.
I would expect the below code to generate (quasi) random numbers between 0.9 and 1.0 for RH.
randno5=$((RANDOM % 100001))
upper_limit5=$(echo "scale=10; 1*1.0"|bc)
lower_limit5=$(echo "scale=10; 1*0.9"|bc)
range5=$(echo "scale=10; $upper_limit5-$lower_limit5"|bc)
RH=`echo "scale=10; ${lower_limit5}+${range5}*${randno5}/100001" |bc`
However, when I run this code I get value between 0.9 and 0.933(3sf). Why is this the case?

$RANDOM is, at most, 32767:
RANDOM Each time this parameter is referenced, a random integer between
0 and 32767 is generated. The sequence of random numbers may be
initialized by assigning a value to RANDOM. If RANDOM is unset,
it loses its special properties, even if it is subsequently
reset.
Your modulus will have no effect as all generated numbers will be restricted to that range.

Related

Generating a random number between 0.5 and 1.5 in bash [duplicate]

This question already has answers here:
How to generate a random decimal number from 0 to 3 with bash?
(2 answers)
Generate random float number in given specific range of numbers using Bash
(3 answers)
Closed 2 years ago.
I need random numbers to three decimal places like:
0.624, 1.035, 0.869, 1.324
What I am using is:
"0.$(($RANDOM%1000+500))"
However, in this case, all the values less than 1 are correct (i.e. 0.917,0.917,0.917,0.855), but the values greater than 1 are incorrect (i.e 0.1195,0.14340.1434) as I append 0. At the beginning of the random number produced.
Thanks.

Adding padded zeros to name files [duplicate]

This question already has answers here:
How to zero pad a sequence of integers in bash so that all have the same width?
(15 answers)
Closed 2 years ago.
Based off this: How to zero pad a sequence of integers in bash so that all have the same width?
I need to create new file names to enter into an array representing chromosomes 1-22 with three digits (chromsome001_results_file.txt..chromsome022_results_file.txt)
Prior to using a three digit system (which sorts easier) I was using
for i in {1..22};
do echo chromsome${i}_results_file.txt;
done
I have read about printf and seq but was wondering how they could be put within the middle of a loop surrounded by text to get the 001 to 022 to stick to the text.
Many thanks
Use printf specifying a field with and zero padding.
for i in {1..22};
do
printf 'chromsome%03d_results_file.txt\n' "$i"
done
In %03d, d means decimal output, 3 means 3 digits, and 0 means zero padding.

Why does ruby round like this? [duplicate]

This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 8 years ago.
Hi I have the following question:
When I put (1+7/100) in ruby it gives 1.
This is very strange because normally this is how I calculate a 7% increase in Excel.
But when I put (1+7.0/100) it gives me 1.07 which is the correct answer I expected.
Why is ruby doing this? And how do you solve this issue in your calculations in ruby?
This has nothing to do with rounding.
Ruby does division differently on float than it does on an integer.
If you divide integers, you will always get an integer result.
If you divide with floats (or a mixture of integer and float), you will always get a float result.
Remember your order of operations, too. Ruby is going to handle the division before it handles the addition.
7/100 = 0 so 1+0 = 1
7.0/100 = 0.07 so 1+0.07 = 1.07

Arithmetic with variable [duplicate]

This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Closed 8 years ago.
I need to perform some arithemic with bash.It goes like this
VariableA = (VariableB-VariableC) / 60
Variable A should be approximated to 2 decimal places
I don't know which one of these is the right answer(Don't have a linux server at hand atm to test)
VariableA = $((VariableB-VariableC)/60)
VariableA = $(((VariableB-VariableC)/))/60)
It would be nice if someone could also help me out about how to round the VariableA to 2 decimal places without using third party tools like bc
The bash itself can compute only integer values, so if you need to use a fixed number of decimals, you can shift your decimal point (it's like computing in cents instead of dollars or euros). Then only at the output you need to make sure there's a . before the last two digits of your number:
a=800
b=300
result=$((a*100/b)) # factor 100 because of division!
echo "${result:0:-2}.${result: -2}"
will print 2.66.
If you want to make computations in floating points, you should use a tool like bc to do that for you:
bc <<<'scale=2; 8.00/3.00'
will print out 2.66.

Print integer with "most appropriate" kilo/mega/etc multiplier [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert byte size into human readable format in java?
Given an integer, I'd like to print it in a human-readable way using kilo, mega, giga etc. multipliers. How do I pick the "best" multiplier?
Here are some examples
1 print as 1
12345 print as 12.3k
987654321 print as 988M
Ideally the number of digits printed should be configurable, e.g. in the last example, 3 digits would lead to 988M, 2 digits would lead to 1.0G, 1 digit would lead to 1G, and 4 digits would lead to 987.7M.
Example: Apple uses an algorithm of this kind, I think, when OSX tells me how many more bytes have to be copied.
This will be for Java, but I'm more interested in the algorithm than the language.
As a starting point, you could use the Math.log() function to get the "magnitude" of your value, and then use some form of associative container for the suffix (k, M, G, etc).
var magnitude = Math.log(value) / Math.log(10);
Hope this helps somehow

Resources