View Problem

Generate a random integer in a given range

Produce a random integer between 100 and 200 inclusive
DiskEdit
ruby
randomInt = rand(200-100+1)+100;
DiskEdit
ruby >= 1.9.3
rand(100..200)
ExpandDiskEdit
java
Random random = new Random();
int randomInt = random.nextInt(200-100+1)+100;
DiskEdit
perl
my $range = 100;
my $minimum = 100;

my $random_number = int(rand($range)) + $minimum;

print "$random_number\n";
ExpandDiskEdit
groovy
random = new Random()
randomInt = random.nextInt(200-100+1)+100
ExpandDiskEdit
scala
val rnd = new GenRandInt(100, 200)
val randomInt = rnd.next
ExpandDiskEdit
scala
val rnd = new scala.util.Random
val range = 100 to 200
println(range(rnd.nextInt(range length)))
ExpandDiskEdit
scala
val start = 100
val end = 200
val rnd = new scala.util.Random

println( start + rnd.nextInt( (end - start) + 1 ) )
DiskEdit
python
import random
random.randint(100, 200)
DiskEdit
python 2.4
# uses best entropy source available (e.g. /dev/urandom, CryptGenRandom, ...)

import random
print random.SystemRandom().randint(100,200)
ExpandDiskEdit
cpp C++/CLI .NET 2.0
Random^ rnd = gcnew Random;
int rndInt = rnd->Next(100, 201);
ExpandDiskEdit
cpp
std::srand(std::time(NULL));

unsigned lb = 100, ub = 200;
unsigned rnd = lb + (rand() % ((ub - lb) + 1));
ExpandDiskEdit
cpp
typedef boost::uniform_int<> Distribution;
typedef boost::mt19937 RNG;

Distribution distribution(100, 200);
RNG rng; rng.seed(std::time(NULL));
boost::variate_generator<RNG&, Distribution> generator(rng, distribution);

unsigned rnd = generator();
ExpandDiskEdit
fsharp
let rnd = new Random()
let rndInt = rnd.Next(100, 201)
ExpandDiskEdit
erlang
RandomInt = gen_rand_integer(100, 200),
DiskEdit
erlang R18
99 + rand:uniform(101).
DiskEdit
ocaml
Random.self_init ();;
let a = 100 and b = 200 in
Random.int ( b - a + 1 ) + a;;
DiskEdit
csharp
System.Random r = new System.Random();
int random = r.Next(100,201);
ExpandDiskEdit
php
$r = mt_rand(100, 200);
DiskEdit
haskell
import System.Random

randInRange :: Int -> Int -> IO Int
randInRange a b = getStdRandom $ randomR (a, b)

main = randInRange 100 200 >>= print
DiskEdit
haskell
import System.Random

main = randomRIO (1,100) >>= print
DiskEdit
clojure
(+ (rand-int (- 201 100)) 100)
ExpandDiskEdit
fantom
r := Int.random(100..200)
ExpandDiskEdit
go
rand.Intn(100) + 100

Submit a new solution for ruby, java, perl, groovy ...