• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
April 13, 2023April 13, 2023

Class Random in Java

Class random in java

The Random class is a pseudo-random number generator. Pseudo-random numbers are generated by starting with a seed value and then using an algorithm to generate a sequence of numbers that appear to be random. The Random class uses a 48-bit seed and a linear congruential algorithm to modify the seed.

As a consequence of this implementation, two Random instances that are constructed with the same seed value generate exactly the same sequence of numbers.

The Random class provides methods that return pseudo-random values for various primitive Java types. The Math.random() method is easier to use if we do not need to fine-tune the generation of random numbers.

For an example of this class Random in java is used to generate a stream of pseudo-random numbers.

However, the class uses a 48-bit seed, which is modified using a linear congruent formula.

If two instances of Random are created with the same seed and the same sequence of a method,  calls are made from each, hence they will generate and return an identical sequence of numbers.

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each.

They will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random.

Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code.

However, subclasses of the class Random are permitted to use other algorithms. So long as they adhere to the general contracts for all the methods.

Create a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by the method next(int).

The invocation new Random(seed) is equivalent to:

The class structure of Random  is given as:

public class java.util.Random extends java.lang.Object implements java.io.Serializable{
//constructors
public Random()//creates a new Random number generator.Its seed is initialized to a value based on the current time.
public Random(long seed)//creates a new Random number generator using a long seed.
//methods
public double nextDouble();//returns values between 0.0 to 1.0.
public int nextInt();//nextInt() generates int values through out the range of this data types.
public float nextFloat();//returns values between 0.0 to 1.0.
public long nextLong();//nextLong() generates long values through out the range of this data types.
public void nextBytes(byte[] bytes);
public synchronized double nextGaussian();//returns the pseudo random values with Gaussian distribution.the mean value is 0.0 and standard deviation is 1.0 
public synchronized void setSeed(long seed);// this helps to initialize the pseudo random number generator with some  variable seed (not the current time- it is default though) or with a constant to generate repeatable sequence of pseudo-random numbers
protected synchronized int next(int bits);

}

The details of the class structure are given as follows:

public Random();

public Random() constructor creates a Random object with the current time as its seed value.

public Random(long seed);

public Random(long seed) constructor creates a Random object with the given seed value.

Parameter
seed – The seed value to use.

public void nextBytes(byte[] bytes); 

public void nextBytes(byte[] bytes); method fills the given array with pseudo-random byte values.

Parameter
bytes – The byte array to fill.

public double nextDouble();

public double nextDouble() method returns the next pseudo-random, uniformly distributed double value. The value is
between 0.0 and 1.0 inclusive.

This method returns the next pseudo-random double value.

public float nextFloat();

public float nextFloat() method returns the next pseudo-random, uniformly distributed float value. The value is
between 0.0 and 1.0 inclusive.

This method returns the next pseudo-random float value.

public synchronized double nextGaussian();

public synchronized double nextGaussian() method returns the next pseudo-random, Gaussian distributed double value. The value has a mean of 0.0 and a standard deviation of 1.0 from the random number sequence. The value is between -1.0 and 1.0.

This method returns the next pseudo-random double value.

public int nextInt();

public int nextInt() method returns the next pseudo-random, uniformly distributed int value.

This method returns the next pseudo-random int value.

public long nextLong();

public long nextLong() method returns the next pseudo-random, uniformly distributed long value.

This method returns the next pseudo-random long value.

public synchronized void setSeed(long seed);

public synchronized void setSeed(long seed) method sets this Random object’s seed value to the given value.

Parameter
seed – The new seed value.

protected synchronized int next(int bits);

protected synchronized int next(int bits) method generates the given number of bits and returns them as an integer value. A subclass of Random should override this method, as it is used by all of the other random-number generation
methods in the class.

This method returns the specified number of pseudo-random bits.

Parameter
bits – The number of bits to generate.

Apart from these Random class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Zoom Meeting Outfits: 4 Comfortable Yet Dressy Tops to Wear on Your Next Video Call
  • Zoom Clone: 6 Significant Benefits To Lure You In Launching
  • Your Shopify Store – Top 5 Essential Inventory Management Apps
  • All-inclusive You Need To Know About Skirting Beyond 2020
  • Yohuton Health and Fitness Smartwatch for Men Beyond 2020

Categories

  • Adventure
  • Agile Methodology
  • AI
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Companies
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Earn Money Online
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Graphic Designers
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Magento
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Office Productivity
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • RDT
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOA
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VBscript
  • VPN
  • Web Development
  • WebHosting
  • WebServices
  • Wildlife
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948