Random Number Generation In Various Programming Languages

JavaScript

JavaScript is one of the popular programming languages. JavaScript is a universal programming language which shows a great level of flexibility and performance. It is free to use, easy to learn, and easy to implement (both in web and hybrid mobile applications). All these reasons combined make it the perfect cross-platform programming language.

var rand = Math.random();
var rand = Math.floor(Math.random * N) + 1;

Java

Java is one of the “slowly dying” programming languages. But even today, it has many applications as compared to others. The key features of Java are speed, safety, and cross-platform applications. The main areas of Java include Android App development, Software development and much more.

double rand = Math.random();
Random rand = new Random();int r1 = rand.nextInt(N) + 1;

C++

Here’s the trouble! But first, let me introduce this guy.

int r = rand() % N + 1;
for (int i = 0; i < 5; i++)
cout << rand() % 5 << endl;
#include <iostream>
#include <time.h>
using namespace std;int main() {
srand(time(NULL));
for (int i = 0; i < 5; i++) {
cout << rand() % 5 << endl;
}
return 0;
}

Python

The language of the future is here! Python is a high-level programming language which can be used as a scripting language or for object-oriented programming. Python can do anything. There are so many libraries, both built-in and third-party, which help Python do anything from as simple as writing “Hello World!” in one line to become one of the best language for implementing machine learning algorithms.

import random
print random.randint(0, 5)
random.random() * 100
import randommyList = [2, 109, False, 10, “Lorem”, 482, “Ipsum”]random.choice(myList)
#random element from list
random.shuffle(list)
#shuffled the list
#example = [109, 2, 10, “Loren”, False, “Ipsum”, 482]

Swift

The programming language Swift was developed by Apple to incorporate and improve many features of the programming language Objective-C. Swift is a modern language and an easy tool to use. It is the foundation of iOS development.

let number = Int.random(in: 0 … 10)
let number = Int.random(in: 0 ..< 10)
let b = Bool.random()
let number = Float.random(in: 0 ..< 1)
let names = [“Ford”, “Zaphod”, “Trillian”, “Arthur”, “Marvin”]let randomName = names.randomElement()
//got a random element from `names`
names.shuffle()
//`names` can now be: [“Zaphod”, “Marvin”, “Arthur”, “Ford”, “Trillian”]

C#

C# is very similar to Java but is much more difficult. It is one of the most advanced and convenient programming languages around. It is used to create Windows applications, XML Web services, client-server applications and database applications.

Random rnd = new Random();int month = rnd.Next(1, 13); // creates a number between 1 and 12int dice = rnd.Next(1, 7); // creates a number between 1 and 6int card = rnd.Next(52); // creates a number between 0 and 51

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store