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

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.

Related

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.

How can I generate a random number between 0-5 in Swift? [duplicate]

This question already has an answer here:
How to generate a random number in a range (10...20) using Swift [duplicate]
(1 answer)
Closed 6 years ago.
I have only been coding in my off time for a couple of weeks now and am creating an app with a very basic "guess how many" concept. The user will enter a number between 0-5 and the app will tell the user whether they are right or wrong.
How would I generate a number between 0-5?
You can use any random method and performing modulo operator on it
let randomNumber = random() % 6

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

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.

Does grouping numbers with parentheses in Ruby not work? [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.
I was trying to write a small program in Ruby, and I ran into the following problem: Ruby doesn't seem to be able to group numbers in parentheses.
For example:
puts (2 - 0) / 10
# prints out 0
There is obviously a flaw in the logic here. What should be happening is that (2 - 0) gets evaluated first (according to the order of operations) and then (2 - 0) should get divided by 10.
Does grouping with parentheses in Ruby not work? By the way, I'm using 2.1.2.
You're doing integer division without realizing it. 2 / 10 does equal 0 in integer division.
Try instead running this:
puts (2 - 0) / 10.0
# prints out 0.2
You will probably get an answer more like what you're expecting. The reason is that by changing 10 to 10.0, you coerce the operation into floating point division.

Lua math.random? [duplicate]

This question already has answers here:
Generating uniform random numbers in Lua
(4 answers)
Closed 8 years ago.
I've been having trouble generating a random number for a while now using Lua. I tried starting the script with math.randomseed(os.time()) and I was still getting the same results. How would I get the script below to generate a new random number every time I run the script?
function rand()
local x = math.random(1, #Questions) --Pick a random question from a table
return x
end
This is a well known problem. Just call math.random once or twice before using the result in your program.

Resources