Generate Random number

Here given code implementation process.

//C Program
//Print random number
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//Print random number in given size
void simple_random(int size)
{
  int number = 0;
  int i =0;
  while(i<size)
  {
    //Get new rand number
    number = rand();
    printf("%d\n",number ); 
    i++;
  }
}
void random_between_range(int first,int last)
{
  //Calculate random number
  int number = rand()%((last+1)-first) + first;
  printf("%d\n",number);
}

int main() {
  //impotant sets the seed based on the current time
  srand(time(NULL));  

  //Test Case
  simple_random(3);

  
  //Range from 1 to 10
  random_between_range(1,10);
  //Range from 50 to 100
  random_between_range(50,100);
  //Range from 1000 to 2000
  random_between_range(1000,2000);
  return 0;
}

Output

83707407
295259376
1183202048
7
100
1379
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
/*
 C++ Program
 Print random number
*/
class MyNumber {
	public:

		//Print random number in given size
		void simple_random(int size) {
			int number = 0;
			int i = 0;
		
			while (i < size) {
				//Get new rand number
				number = rand();
				cout << number << "\n";
				i++;
			}
		}
	void random_between_range(int first, int last) {
		//Calculate random number
		int number =  rand()%((last+1)-first) + first;
		cout << number << "\n";
	}
};
int main() {
	MyNumber obj;
	//Test Case
	obj.simple_random(3);
	//Range from 1 to 10
	obj.random_between_range(1, 10);
	//Range from 50 to 100
	obj.random_between_range(50, 100);
	//Range from 1000 to 2000
	obj.random_between_range(1000, 2000);
	return 0;
}

Output

1804289383
846930886
1681692777
6
82
1521
/*
  Java Program
  Print random number
*/
import java.util.Random; 
public class MyNumber {

  //Print random number in given size
  public void simple_random(int size)
  {
    int number = 0;
    int i =0;
    Random rand = new Random(); 
    while(i<size)
    {
      //Get new rand number
      number =rand.nextInt();
      System.out.print(number+"\n"); 
      i++;
    }
  }
  void random_between_range(int min,int max)
  {
    //Calculate random number
    int number =  min + (int)(Math.random() * ((max - min) + 1));
    System.out.print(number+"\n"); 
  }


  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
  
      //Test Case
      obj.simple_random(3);

      
      //Range from 1 to 10
      obj.random_between_range(1,10);
      //Range from 50 to 100
      obj.random_between_range(50,100);
      //Range from 1000 to 2000
      obj.random_between_range(1000,2000);
  }
}

Output

1031058539
-785509332
749651194
7
71
1575
/*
  C# Program
  Print random number
*/
using System;
public class MyNumber {

	//Print random number in given size
	public void simple_random(int size) {
		int number = 0;
		int i = 0;
		Random rand = new Random();
		while (i < size) {
			//Get new rand number
			number = rand.Next();
			Console.Write(number + "\n");
			i++;
		}
	}
	void random_between_range(int min, int max) {
		Random rand = new Random();
		//Calculate random number
		int number = rand.Next(min, max);;
		Console.Write(number + "\n");
	}


	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		//Test Case
		obj.simple_random(3);


		//Range from 1 to 10
		obj.random_between_range(1, 10);
		//Range from 50 to 100
		obj.random_between_range(50, 100);
		//Range from 1000 to 2000
		obj.random_between_range(1000, 2000);
	}
}

Output

366740596
1567720781
2116401671
6
54
1014
# Python 3 Program
# Print random number

import random
import sys


class MyNumber :
  #Print random number in given size
  def simple_random(self, size) :
    number = 0
    i = 0
    while (i < size) :
      #Get new rand number
      number = random.randint(0,sys.maxsize-2)
      print(number)
      i += 1
    
  
  def random_between_range(self, first, last) :

    #Calculate random number
    number = random.randint(first,last)
    print(number)
  

def main() :
  obj = MyNumber()
  #Test Case
  obj.simple_random(3)
  #Range from 1 to 10
  obj.random_between_range(1, 10)
  #Range from 50 to 100
  obj.random_between_range(50, 100)
  #Range from 1000 to 2000
  obj.random_between_range(1000, 2000)


if __name__ == "__main__":
  main()

Output

4908962921347846998
8226439082434031005
8147026621081844759
9
71
1181
# Ruby Program 
# Print random number


class MyNumber 
	#Print random number in given size
	def simple_random(size) 
		number = 0
		i = 0
		r = Random.new
		max_int = (2**(0.size * 8 -2) -1)
		while (i < size) 
			#Get new rand number
			number = r.rand(0...max_int)
			print(number ,"\n")
			i += 1
		end
	end
	def random_between_range(first, last) 
		r = Random.new
		#random number
		number = r.rand(first...last)
		print(number ,"\n")
	end
end
def main() 
	obj = MyNumber.new()
	#Test Case
	obj.simple_random(3)
	#Range from 1 to 10
	obj.random_between_range(1, 10)
	#Range from 50 to 100
	obj.random_between_range(50, 100)
	#Range from 1000 to 2000
	obj.random_between_range(1000, 2000)
end
main()

Output

4568774108073187389
1018769358319947757
3967416525849002481
5
94
1495
/*
 Scala Program
 Print random number
*/

class MyNumber {
	//Print random number in given size
	def simple_random(size: Int): Unit = {
		var number: Int = 0;
		var i: Int = 0;
		val r = new scala.util.Random
		while (i < size) {
			//Get new rand number
			number = r.nextInt();
			print(s"$number\n");
			i += 1;
		}
	}
	def random_between_range(first: Int, last: Int): Unit = {
		val r = new scala.util.Random
		//Calculate random number
		var number: Int = first + r.nextInt((last-first)-1) ;
        print(s"$number\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.simple_random(3);
		//Range from 1 to 10
		obj.random_between_range(1, 10);
		//Range from 50 to 100
		obj.random_between_range(50, 100);
		//Range from 1000 to 2000
		obj.random_between_range(1000, 2000);
	}
}

Output

-693473602
-85791068
38508938
8
59
1700
/*
  Swift 4 Program
  Print random number
*/
import Foundation

#if os(Linux)
    srandom(UInt32(time(nil)))
#endif
class MyNumber {
	//Print random number in given size
	func simple_random(_ size: Int) {
		var number: Int = 0;
		var i: Int = 0;
      	let max_value = UInt8.max-1 ;
		while (i < size) {
			//Get new rand number
			#if os(Linux)
              number = Int(random()) 
            #else
              number = Int(arc4random_uniform(UInt32(max_value)))
            #endif
			print(number );
			i += 1;
		}
	}
	func random_between_range(_ min: Int, _ max: Int) {
		
	 //Calculate random number
	  var number: Int = 0;
      #if os(Linux)
      	number = min + Int(random()%(max-min)) 
      #else
      	number =  Int(min + arc4random_uniform(max - min)) 
      #endif
	  print(number);
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.simple_random(3);
	//Range from 1 to 10
	obj.random_between_range(1, 10);
	//Range from 50 to 100
	obj.random_between_range(50, 100);
	//Range from 1000 to 2000
	obj.random_between_range(1000, 2000);
}
main();

Output

1804289383
846930886
1681692777
6
83
1335
<?php
/*
  Php Program
  Print random number
*/
class MyNumber {
	//Print random number in given size

	public 	function simple_random($size) {
		$number = 0;
		$i = 0;
		
		while ($i < $size) {
			//Get new rand number
			$number =rand();
			echo($number ."\n");
			$i++;
		}
	}

	function random_between_range($first, $last) {
	
		//Calculate random number
		$number = rand($last, $first);
		echo($number ."\n");
	}
};

function main() {
	$obj = new MyNumber();
	//Test Case

	$obj->simple_random(3);
	//Range from 1 to 10

	$obj->random_between_range(1, 10);
	//Range from 50 to 100

	$obj->random_between_range(50, 100);
	//Range from 1000 to 2000

	$obj->random_between_range(1000, 2000);
}
main();

Output

30943200
88506239
1099877525
3
68
1137
/*
 Node Js Program
*/


class MyNumber {
	//Print random number in given size
	simple_random(size) {
		var number = 0;
		var i = 0;
	
		while (i < size) {
			//Get new rand number
			number =  Math.floor(Math.random() * (Number.MAX_VALUE-1));
			process.stdout.write(number + "\n");
			i++;
		}
	}
	random_between_range(min, max) {
	
		//Calculate random number
		var number = Math.floor(Math.random() * (max - min) + min);
		process.stdout.write(number + "\n");
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Case
	obj.simple_random(3);
	//Range from 1 to 10
	obj.random_between_range(1, 10);
	//Range from 50 to 100
	obj.random_between_range(50, 100);
	//Range from 1000 to 2000
	obj.random_between_range(1000, 2000)
}
main();

Output

1.4242225289803204e+308
1.2450315359389768e+307
1.6263924192162678e+308
8
58
1313


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved