Languages
[Edit]
EN

Java - own random number generator (custom implementation)

14 points
Created by:
Inferio
328

1. Introduction

Edit

In java we can implement custom random number generator by using LCG (Linear congruential generator) algorithm. LCG is one of the oldest and best-known pseudorandom number generator algorithm. We can adjust this implementation to work on int or long.

Linear congruential generator is defined by recurrence relation:

Where:

  • Xn+1 - new seed
  • a - MULTIPLIER_A
  • Xn - current seed field
  • c - INCREMENT_C
  • m - MODULUS

In our below implementation above variables were used.

2. Implementation in java

Edit

Note:

Every time randomNumber() method is called, we assign new value to seed.

Output:

Explanation

Edit

Below 2 fields:

are used by runtime libraries of various compilers:

  • glibc (used by GCC)
  • ANSI C: Watcom, Digital Mars, CodeWarrior, IBM VisualAge C/C++
  • C90, C99, C11: Suggestion in the ISO/IEC 9899, C18

3. Probability distribution test

Edit

Output:

4. JDK Random class - probability distribution test

Edit

Output:

References

Edit
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - random numbers

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join