import java.util.Random;
public class Console {
public static void main(String[] args) {
while (3>2) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
Random rand5 = new Random();
Random rand6 = new Random();
Random rand7 = new Random();
Random rand8 = new Random();
int onenum = rand1.nextInt(2);
int twonum = rand2.nextInt(2);
int threenum = rand3.nextInt(2);
int fournum = rand4.nextInt(2);
int fivenum = rand5.nextInt(2);
int sixnum = rand6.nextInt(2);
int sevennum = rand7.nextInt(2);
int eightnum = rand8.nextInt(2);
int binary[] = {onenum, twonum, threenum, fournum, fivenum, sixnum, sevennum, eightnum};
System.out.println(java.util.Arrays.toString(binary));
How can I check if, say the first number of the binary array is one?
Currently if I run, i get an output of like {1, 0, 1} ect
You can do it by using an index to access to an element in a specific position of an array:
if (someArray[position] == something)
In your case, to check the first element, it would be:
if (binary[0] == 1)
Note:
Indices in most programming languages start with 0 so the first element would be in the index 0 of the array. The second in the index 1. And so on.
Gosh i can reduce your code so much!! Analyse and see that this code does same as yours.
while (3>2) {
Random rand = new Random();
int[] binary = new int[8];
for(int i=0;i<8;i++)
binary[i] = rand.nextInt(2);
if(binary[0] == 1)
//if first number is 1
}
You can simply do:
if(binary[0] == 1) {
//do something
}
0 is the first element in array, since the fact of Arrays is a 0 based.
Side note: You don't have to declare a new object from Random to get a number, you can use the same object like:
Random rand = new Random();
int onenum = rand.nextInt(2);
int twonum = rand.nextInt(2);
int threenum = rand.nextInt(2);
// and so on
Just do this to check if first element equals to 1:
if (binary[0] == 1)
{
//do whatever here..
}
Other issues about your codes
I realized you use while (3>2). You can use while(true) if you want it to be an infinite loop.
You only need to create one random object to create multiple random numbers. Do it like this..
.
Random rnd = new Random();
oneNum = rnd.nextInt(2);
twoNum = rnd.nextInt(2);
threeNum = rnd.nextInt(2);
You can even do it this way (using an array to store the random numbers):
Random rnd = new Random();
int[] num = new int[8];
for (int x=0; x<num.length; x++)
num[x] = rnd.nextInt(2);
Related
Here is the code:
Random randomNumber = new Random();
System.out.println(randomNumber.nextInt(10));
Random randomNumberTwo = new Random();
System.out.println(randomNumberTwo.nextDouble());
System.out.println(randomNumber + randomNumberTwo);
The terminal says: java: bad operand types for binary operator '+'
randomNumber and randomNumberTwo are generators, so in order to add them you need to generate the next value from them:
Random randomNumber = new Random();
Random randomNumberTwo = new Random();
System.out.println(randomNumber.nextInt(10) + randomNumberTwo.nextDouble());
Here you are adding the objects of the class random numbers instead to this
Random randomNumber = new Random();
int a = randomNumber.nextInt(10)
System.out.println(randomNumber.nextInt(10));
Random randomNumberTwo = new Random();
double b = randomNumberTwo.nextDouble()
System.out.println(randomNumberTwo.nextDouble());
System.out.println(a + b);
I'm trying to randomize between these 3 variables (not range, but only between these 3 values) and store it into new variable.
int randomProductDiscount() {
int disc1 = 25;
int disc2 = 35;
int disc3 = 50;
int productDiscount = (random between disc1 or disc2 or disc3);
return productDiscount;
}
Any help would be appreciated.
Put them in an array and obtain random index:
static Random rand = new Random();
int randomProductDiscount()
{
int[] disc = {25,35,50};
return disc[rand.nextInt(disc.length)];
}
This can be used for any number of values you wish to choose randomly from.
My question concerns the Java RNG; use the following code:
for (int s = 0; s < 600; s++) {
Random r = new Random(s);
System.out.println(r.nextDouble());
System.out.println(r.nextDouble() + "\n-----");
}
This will result in 600 random numbers being generated. I know this is a bit odd, but I require a new random number generator each time in my actual project. The seed I receive is sequential. The first random double that is generated is extremely close for any of the seeds, is this because of the linear congruential formula that is used as initialization?
The second double generated actually looks like it is actually properly random, is this safe to assume so? Is it OK practice to first generate an unused random number, and after that moment start to use it for the actual reason it was created?
Thank you in advance
EDIT:
Let me clarify:
int possibleRoutes = 7;
void handlePacket(Packet p) {
int chosenRoute = p.hash % possibleRoutes;
// ...Other code...
}
vs.
int possibleRoutes = 7;
void handlePacket(Packet p) {
Random r = new Random(p.hash);
int chosenRoute = r.nextInt() % possibleRoutes;
// ...Other code...
}
}
vs.
int possibleRoutes = 7;
void handlePacket(Packet p) {
Random r = new Random(p.hash);
r.nextInt();
int chosenRoute = r.nextInt() % possibleRoutes;
// ...Other code...
}
A guarantee is that each packet must take the same route. The packet hash is inherently sequential at the moment. There are too many possible hashes to keep any type of state to speed this up.
why do you give a special number as a seed? just leave it empty, so the Random constructor will choose a seed for you.
for (int s = 0; s < 600; s++) {
Random r = new Random();
System.out.println(r.nextDouble());
System.out.println(r.nextDouble() + "\n-----");
}
see Role of seed in random number generation
Call the default constructor which uses the nanoTime as a seed. This way you won't need to generate a new seed to create your object each iteration.
//loop through number of numbers needed
for(int i = 0; i < 100; i +)
//Calls default constructor
Random r = new Random();
System.out.println(r.nextDouble()*.5);
An alternative is to use a master random to seed all the subsiduary randoms in the loop:
Random masterRand = new Random();
for (int s = 0; s < 600; s++) {
Random r = new Random(masterRand.nextLong());
System.out.println(r.nextDouble());
System.out.println(r.nextDouble() + "\n-----");
}
I need unique random ints in specified range. I use this approch:
class Main
{
static final int RANGE = 100;
static int uniqueGenerator(int range_, boolean boolArr_[], Random rand_)
{
int tmpVar = rand_.nextInt(range_);
while (boolArr_[tmpVar] == true)
{
tmpVar = rand.nextInt(range_);
}
boolArr_[tmpVar] = true;
return tmpVar;
}
public static void main(String[] args)
{
Random rand = new Random();
boolean boolArr[] = new boolean[RANGE];
Arrays.fill(boolArr, false);
int ceiling = 10;
int tmp = Main.uniqueGenerator(ceiling, boolArr, rand);
System.out.println(tmp); => 5
ceiling = 20;
tmp = Main.uniqueGenerator(ceiling, boolArr);
System.out.println(tmp); => 17
}
}
It seems to be cumbersome. Maybe someone knows better approach?
EDIT: I use it in game code, so I need most efficient solution. Answers below suggest initializing new list, shuffling it => too resource consuming/need to generate new list every time when need to change range.
List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i <= 100; i++) list.add(i);
Collections.shuffle(list);
Fill an array with the range of numbers you want, shuffle it and extract an item.
Edit: Look at Eng.Fouad's example to see how this is implemented.
Generate and store random numbers in set
Set<Integer> set = new HashSet<Integer>(100);
Random rand = new Random();
while (set.size() < 1000) {
set.add(rand.nextInt(100));
}
for (Integer integer : set) {
System.out.println(integer);
}
I have been set a task to create a Android app in which the user chooses four numbers (1-6), I then compare it against four randomly generated numbers and then tell them how many of there numbers were correct.
My problem is that whenever I generate any numbers the first three shown are always the same, except from the last number.
Random a1 = new Random();
random1 = new ArrayList<Integer>();
for (int index = 0; index < 6; index++)
{
random1.add(a1.nextInt(5)+ 1);
}
Random a2 = new Random();
random2 = new ArrayList<Integer>();
for (int index = 0; index < 6; index++)
{
random2.add(a2.nextInt(5)+ 1);
}
This is the code I use for the random number generation, each number uses the exact same code, which makes it even more confusing, if they were all the same I could understand that because it's the same code it generates the same number or something along those lines but the last one is always different, any help would always be appreciated.
Try not create two Random instances but reuse single instance instead. May be two Randoms with close seeds produces close output.
Check if below code works for you. Code taken from http://www.javapractices.com/topic/TopicAction.do?Id=62. Modified according to your requirements.
public final class RandomRange {
public static final void main(String... aArgs) {
int START = 1;
int END = 6;
Random random = new Random();
List<Integer> first = new ArrayList<Integer>();
List<Integer> second = new ArrayList<Integer>();
for (int idx = 1; idx <= END; ++idx) {
first.add(showRandomInteger(START, END, random));
second.add(showRandomInteger(START, END, random));
}
System.out.println(first);
System.out.println(second);
first.retainAll(second);//Find common
System.out.println(first);
}
private static int showRandomInteger(int aStart, int aEnd, Random aRandom) {
if (aStart > aEnd) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
// get the range, casting to long to avoid overflow problems
long range = (long) aEnd - (long) aStart + 1;
// compute a fraction of the range, 0 <= frac < range
long fraction = (long) (range * aRandom.nextDouble());
int randomNumber = (int) (fraction + aStart);
return randomNumber;
}
}