DEV Community

Cover image for How do you do random?
David J Eddy
David J Eddy

Posted on • Updated on

How do you do random?

I was watching a VueJS tutorial and was surprised at what it takes in ECMAscript to generate a random integer between 0 and 10.

Math.floor(Math.random() * 10);
Enter fullscreen mode Exit fullscreen mode

In PHP it takes

mt_rand(0, 10);
Enter fullscreen mode Exit fullscreen mode

In your favorite language what does it take to generate an integer between 0 and 10?

Latest comments (48)

Collapse
 
bobnudd profile image
Ash Grennan

C#

var random = new Random();
var i = random.Next(0, 11);

Less elegant than some examples I'll admit

Collapse
 
jckuhl profile image
Jonathan Kuhl

It bothers me that JS has no decent random functions and I have to reach for this bizarre mathy statement to get a random integer from a range.

Really wish we had Python's random module.

I mean, it's not hard to implement, but it's inconvenient.

Collapse
 
programazing profile image
Christopher C. Johnson • Edited

The quick and dirty way in C# to get one random number is:

var randomNumber = new Random().Next(1, 10)

Keep in mind that Random() initializes with the current timestamp so you can't use it in rapid succession and expect a random result.

static void Main(string[] args)
{
     int randomSingle = GetSingleRandomNumber(min: 1, max: 10);

     Console.WriteLine(randomSingle);

     Console.WriteLine(Environment.NewLine);

     List<int> randomList = ListOfRandomNumbers
(min: 1, max: 10, numberOfItems: 5);

     foreach (var number in randomList)
     {
         Console.WriteLine(number);
     }

     Console.ReadKey();
 }

 static int GetSingleRandomNumber(int min, int max)
 {
     return new Random().Next(min, max);
 }

 static List<int> ListOfRandomNumbers
(int min, int max, int numberOfItems)
 {
      var output = new List<int>();

      for (int i = 0; i < numberOfItems; i++)
      {
         output.Add(new Random().Next(min, max));
      }

      return output;
 }

It's important to note what we're doing in the two functions. We're always adding a new Random() i.e

output.Add(new Random().Next(min, max));

Why? Because we get a new timestamp when we do so.

If instead, you did something like this:

var random = new Random();
for (int i = 0; i < numberOfItems; i++)
{
     output.Add(random.Next(min, max));
}

You'd end up with several repeating numbers as it's calculating off the same timestamp. This is fine for small sets of random numbers just don't go newing up Random() for a large set.

Collapse
 
jckuhl profile image
Jonathan Kuhl
const random = (max, min=0) => Math.floor(Math.random() * (max - min)) + min

I wish JS had a more inuitive means of doing random, a built in RandInt function like Python has. Ah well.

Still beats having to make an entire object just for a random number Java.

Collapse
 
cyberianneptune profile image
CyberianNeptune

import random
a = random.randint(0,10)
return a

Collapse
 
larsklopstra profile image
Lars Klopstra ⚡

Math.random() * 10 | 0;

Collapse
 
theqadiva profile image
Theqadiva

I'm a tester and I use random in my automation. In groovy, I use:


import org.apache.commons.lang.RandomStringUtils as RandomStringUtils
WebUI.setText(findTestObject('someWhereToPutNumbers'),RandomStringUtils.randomNumeric(4))

Collapse
 
aritdeveloper profile image
Arit Developer

rand 0..10

Ruby

Collapse
 
scottishross profile image
Ross Henderson

In PL/SQL:

A random number.

select DBMS_RANDOM.random from dual;
--Result: -860942438

A random round number.

select round(DBMS_RANDOM.random) from dual;
--Result: 1096364454

A random number between two values.

select round(DBMS_RANDOM.value(0, 100)) from dual;
--Result: 54
Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Don't trust your own computer for randomness. Better trust a webservice. ;-)

wget -q -O - "https://www.random.org/integers/?num=1&min=1&max=9&col=1&base=10&format=plain&rnd=new"
Collapse
 
learosema profile image
Lea Rosema (she/her)

I had a workshop about random numbers at the CodePenDay 2017 in Hamburg by Bullgit: github.com/bullgit/fair-random

Collapse
 
tmcsquared profile image
TMcSquared • Edited

RPL for HP48/50 series


<< RAND 10 * FLOOR >>

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

APL:

⍝ `? n` generates random number between 1 and n, both inclusive
(? 10) -1
⍝ If we need more than 1 number, use `⍴` function like:
(? 4⍴ 10) -1
⍝ In above code, x⍴y gives you an array of y's of length x

I am a beginner in this language but it is fascinating at how succinct and elegant some constructs are.

Collapse
 
cout970 profile image
cout970

In kotlin

Random.nextInt(0..10)

Or

Random.nextInt(from = 0, until = 11)

Thanks to the Random api in the stdlib this works in the JVM, Kotlin.js and Kotlin native

Collapse
 
gklijs profile image
Gerard Klijs

In clojure it's (rand-int 11) which you can also use in clojurescript.