Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a random number without use of Math.Random?

Tags:

java

random

My project entails that I create a basic number guessing game that uses the JOptionPane and does not use Math.Random to create the random value. How would you go about doing this? I've completed everything except the random number generator. Thanks!

like image 507
John Smith Avatar asked Nov 18 '12 17:11

John Smith


People also ask

What can I use instead of math random in Javascript?

Note: Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.


2 Answers

If you don't like the Math.Random you can make your own Random object.

import:

import java.util.Random;

code:

Random rand = new Random();
int value = rand.nextInt();

If you need other types instead of int, Random will provide methods for boolean, double, float, long, byte.

like image 160
Frank Avatar answered Oct 10 '22 02:10

Frank


In JavaScript using the Middle-square method.

var _seed = 1234;
function middleSquare(seed){
    _seed = (seed)?seed:_seed;
    var sq = (_seed * _seed) + '';
    _seed = parseInt(sq.substring(0,4));
    return parseFloat('0.' + _seed);
}
like image 20
Emmanuel Ulloa Avatar answered Oct 10 '22 03:10

Emmanuel Ulloa