# Generate a random floating point number between 2 limits [closed]

This challenge is to generate a random floating-point number between 2 limits. You may use any language so long as it can be called from the command line, and runs on linux. It must also be able to work with negative numbers.

Due to the comment below: You may use fixed point math to come up with a result in-fact fixed point math is preferable in this case.

# Input Requirements

Accept a lower and upper limit, no verification required.

# Output Requirements

• Must be between the 2 specified limits
• Print it out to the console
• or come up with a way to return it to the shell.

$prog -1 1 -.445  The program would not spit out a result that is not between those 2 limits. And it would be inclusive of the 2 limits # Win Condition As with all of these challenges the smallest number of bytes wins. • Can the first input be greater than the second? – Zgarb Mar 22 '15 at 17:28 • @Zgarb so long as the output remains between the 2 limits – HSchmale Mar 22 '15 at 17:29 • Does the random number have to be uniformly distributed between those limits, or will a non-uniform distribution do? And must every floating point number between those limits be a possible output? (If yes, that technically excludes most standard random number generator APIs, and will require considerable knowledge of the internals of the floating-point format to implement correctly. On the other hand, an unqualified "no" would seem to allow e.g. a solution that only ever returns one of the two limits, never anything in between.) – Ilmari Karonen Mar 22 '15 at 17:42 • In particular, notably, due to the fact that floating-point numbers themselves are not uniformly distributed on the real number line, an RNG cannot satisfy more than two of the following three properties: 1) The distribution of the generated numbers approximates a continuous uniform distribution on the target interval. 2) Every floating-point number within the target interval is generated with a non-zero probability. 3) Every floating-point number that can be generated is generated with the same probability. Thus, you really should specify which (if any) of those properties you require. – Ilmari Karonen Mar 22 '15 at 17:48 ## 5 Answers # Cjam, 9 bytes q~1$-dmr+


Assumes that the first input number is the lower limit and the second one is the upper limit.

Pretty straight forward code:

q~          "Read the input as a string and evaluate it. This puts the two input";
"integers on stack";
1$"Copy the first smaller integer and put it on top of stack"; - "Take difference of smaller and larger integer"; d "Convert the integer to double"; mr "Get a random double float from 0 to the difference of two integers"; + "Add the random double to the smaller integer to get random in range";  Try it online here # Ruby, 34 bytes a,b=$*.map &:to_f
\$><<a+(b-a)*rand


Takes inputs via command-line argument as in the usage example.

# Pyth - 9 bytes

Works the obvious way like all the others.

+*-vzQOZQ


Pyth's random range function when called with zero as the argument acts as a [0, 1) floating point generator.

+          Add and implicitly print
*         Times
-        Difference
vz      Input 1
Q       Input 2
OZ       Float RNG
Q         Input 2

• Nice. Completely missed, that random floats were added last week. – Jakube Mar 22 '15 at 20:31

# R, 34

Setup for running from command-line

i=scan('stdin');runif(1,i[1],i[2])


Takes input from STDIN

Example on windows

C:\Program Files\R\R-3.1.3\bin\x64>rscript -e "i=scan('stdin');runif(1,i[1],i[2])"
1
2
^Z
[1] 1.249665

C:\Program Files\R\R-3.1.3\bin\x64>

• As a note to the OP: When running an R script which reads from stdin on the command line, you have to submit Ctrl+Z (or Cmd+Z on Mac) to get it to stop reading from stdin. – Alex A. Mar 22 '15 at 19:44

# C#, 142 141 bytes

C# has too much boilerplate...
Anyways, it is the only language I know and am sure can be compiled and run in Linux.

using System;class A{static void Main(string[]a){var b=double.Parse(a[0]);Console.Write(new Random().NextDouble()*(double.Parse(a[1])-b)+b);}


Ungolfed:

using System;
class A
{
static void Main(string[] a)
{
var b = double.Parse(a[0]);
Console.Write(new Random().NextDouble() * (double.Parse(a[1]) - b) + b);
}
}


Also, I just noticed that every example's input was integral. In that case...

# C#, 135 bytes, integral input

using System;class A{static void Main(string[]a){int b=int.Parse(a[0]);Console.Write(new Random().NextDouble()*(int.Parse(a[1])-b)+b);}


Ungolfed:

using System;
class A
{
static void Main(string[] a)
{
int b = int.Parse(a[0]);
Console.Write(new Random().NextDouble() * (int.Parse(a[1]) - b) + b);
}
}

• Just FYI, yes, C# can be compiled and run on Linux, but it requires Mono. – Alex A. Mar 22 '15 at 19:38