Generate a random floating point number between 2 limits

-3

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.

Example

$ 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.

HSchmale

Posted 2015-03-22T17:17:25.903

Reputation: 181

Question was closed 2015-03-23T18:16:04.580

Can the first input be greater than the second? – Zgarb – 2015-03-22T17:28:16.747

@Zgarb so long as the output remains between the 2 limits – HSchmale – 2015-03-22T17:29:57.767

5Does 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 – 2015-03-22T17:42:27.893

4In 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 – 2015-03-22T17:48:05.447

Answers

4

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

Optimizer

Posted 2015-03-22T17:17:25.903

Reputation: 25 836

3

Ruby, 34 bytes

a,b=$*.map &:to_f
$><<a+(b-a)*rand

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

Martin Ender

Posted 2015-03-22T17:17:25.903

Reputation: 184 808

3

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

Try it here.

Maltysen

Posted 2015-03-22T17:17:25.903

Reputation: 25 023

Nice. Completely missed, that random floats were added last week. – Jakube – 2015-03-22T20:31:36.177

1

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
Read 2 items
[1] 1.249665

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

MickyT

Posted 2015-03-22T17:17:25.903

Reputation: 11 735

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. – 2015-03-22T19:44:18.880

0

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);
    }
}

LegionMammal978

Posted 2015-03-22T17:17:25.903

Reputation: 15 731

Just FYI, yes, C# can be compiled and run on Linux, but it requires Mono. – Alex A. – 2015-03-22T19:38:09.563