Still fairly new to android. I am trying to learn how to create a random number that has 12 characters, and each time a number is randomly created, it saves to the android database. Please any help with giving me an idea on how to do this would be grateful.
Use
Math.random();
//Complete rnd float between 0 and 1 e.g.
//0.2558734951799192
//0.03857502479001995
//0.08562741500057713
//0.2329257841571789
//0.7471882261881438
or
Random rand = new Random();
int rndNumber = rand.nextInt(49);
//Numbers between 0 and 48
--
So 12 digit random number can be done with both
float random = 100000000000 + Math.random() * 899999999999;
or
Random rand = new Random();
float rndNumber =100000000000 + rand.nextFloat(899999999999)+1;
--
Edited thx to Frizi
You've asked a general question so the best I can do is give you a general answer. You can use the Random class in java to generate your random numbers. For information on how to then store it to a database, chekcout this documentation.
Related
I've been making a little game in Java and I'm using the Java Random class to generate random number.
During the course of the game, two teams play one another, and to figure out who wins the match, I generate two random numbers (from 1 to 10, like a d10) and then do some modifications to those scores, comparing them to find out who wins.
But I was wondering if there was a more efficient way to do it. I don't really know exactly how the Java Random class works, but my idea was that I would instead generate a single random number (from 1 to 100) and then integer divide by 10 to get one of the numbers and then do mod 10 to find the other number.
In code, my question is which of these is faster:
Random r = new Random();
int team1Score;
int team2Score;
int randNum;
// current version
team1Score = r.nextInt(10) + 1;
team2Score = r.nextInt(10) + 1;
// new version
randNum = r.nextInt(100) + 1;
team1Score = randNum / 10;
team2Score = randNum % 10;
And if, anyone has any ideas, is there any way to make it even more efficient than either of these?
This is an example of premature optimization. The difference between these two examples is incredibly small, and you really shouldn't worry about it. I'd go with the first for the sake of readability.
This question already has answers here:
Java random always returns the same number when I set the seed?
(7 answers)
Closed 2 years ago.
for(int i = 0; i < emojiCnt; i++){
Random rand = new Random(1000);
int randNum = rand.nextInt((3) + 1);
switch (randNum){
case 1:
//Code
case 2:
//Code
case 3:
//Code
default:
//Code
}
randNum = rand.nextInt((10) + 1);
}
Every time I run the code, it gives the same result and is not random? I reassign randNum so that it will go to a random number but it doesn't seem to change?
Random rand = new Random(1000); tells java to create a Random, based on the initial seed of 1000. This results in random, but still reproducable results.
If you want different values for each execution, use Random rand = new Random(); instead.
You seed the RNG with a constant by calling ... = new Random(1000);. With the same seed, one will always get the same sequence of "random numbers". Do not seed (... = new Random();), and the values should be "random".
A comment on randomness and programming:
Without an external entropy generator, a computer is generally not able to generate true randomness. Thus, random number generators operate by using an inital seed to generate a pseudo-random sequence. The sequence normally satisfies all conditions expected by a truly random sequence, but is deterministic once the seed is known. More information can be found on the wikipedia article on Random number generators.
Try to use this code to get random numbers:
Random rand = new Random();
int maxNumber = 1000;
rand.nextInt(maxNumber)
This code will bring you random values between 0 and 1000
I am using java.util.random with a seed(new Random(System.currentTimeMillis())
I need to convert this to a int(my business logic)
I tried using the .nextInt(), but it doesn't help
//my business logic--Below code is in a loop and is intended to
generate a different random number each time//
int randomNumber=(int) Math.floor(inputParam1 * (new Random(System.currentTimeMillis())).nextInt()));
Expected Ouput:A new random number to be generated each time, in int format
Actual Output:
- with nextInt() it is generating the same number each time
- without converting to Int, I am not able to use the 'Random'
datatype with my int variable shown above
Don't create a new instance of Random each time you want to generate a Double.
You can create one instance and then call it whenever you need a new double.
Random rand = new Random(System.currentTimeMillis());
// loop starts here
double randomNumber = Math.floor(inputParam1 * rand.nextDouble());
// If you want an integer up to inputParam1 as it seems, you can do:
int randomInt = (int) randomNumber;
You can also use Math.random() as someone already suggested.
I am not sure why are you casting it to int
double randomNumber= new Random(System.currentTimeMillis()).nextDouble();
this will give a random double number between 0 and 1
Converting below code to int makes no sense.
Math.floor(inputParam1 * (new Random(System.currentTimeMillis())).nextDouble()))
And please, check this answer Using Random Number Generator with Current Time vs Without
The most important part from above link:
If you want your random sequences to be the same between runs you can
specify a seed.
I was trying to explain the random number generator in Java to a friend when he kept getting the same numbers every time he ran the program. I created my own simpler version of the same thing and I too am getting the same exact numbers he was getting every time I run the program.
What am I doing wrong?
import java.util.*;
public class TestCode{
public static void main(String[] args){
int sum = 0;
Random rand = new Random(100);
for(int x = 0; x < 100; x++){
int num = (rand.nextInt(100)) + 1;
sum += num;
System.out.println("Random number:" + num);
}
//value never changes with repeated program executions.
System.out.println("Sum: " + sum);
}
}
The final five numbers out of the 100 are:
40
60
27
56
53
You have seeded the random generator with a constant value 100. It's deterministic, so that will generate the same values each run.
I'm not sure why you chose to seed it with 100, but the seed value has nothing to do with the range of values that are generated (that's controlled by other means, such as the call to nextInt that you already have).
To get different values each time, use the Random constructor with no arguments, which uses the system time to seed the random generator.
Quoting from the Javadoc for the parameterless Random constructor:
Creates a new random number generator. This constructor sets the seed
of the random number generator to a value very likely to be distinct
from any other invocation of this constructor.
Quoting the actual code in the parameterless Random constructor:
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
This:
Random rand = new Random(100);
You're giving the random number generator the same seed (100) each time you start the program. Give it something like the output from System.currentTimeMillis() and that should give you different numbers for each invocation.
Random number generators are really only pseudo-random. That is, they use deterministic means to generate sequences that appear random given certain statistical criteria.
The Random(long seed) constuctor allows you to pass in a seed that determines the sequence of pseudo-random numbers.
Please see the below code to generate a random number from a pool of random numbers.
Random r = new Random(System.currentTimeMillis());
double[] rand = new double[500];
for(int i=0;i<100;i++){
rand[i] = r.nextDouble();
}
double random_number = rand[randomInt];
So let's say you want to generate a random number, but you want it to be UNDER a specified amount.
Is this possible?
Random rand = new Random();
int randInt = rand.nextInt( 16 ); //Generates a number in [0, 1, .., 15]
Documentation at:
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int)
Use the following method random.nextInt(upperBound).
The answers provided here are correct if you are looking for an integer. However, if you are not looking for an integer random number, I think the below solution would work.
If you want a random number between 50 and 100, use this:
randomNumber = 50+(Math.random()*50);