I know this question has been answered out there, but those solutions don't fit in with the way I'm going about, so I'm enquiring to see if there is a simpler solution.
I'm using the set interface and I need there to be 6 random numbers and you can't have duplicates in the set interface.
This is what I've currently got, the way I I'm doing it is not ideal and often causes crashes.
public void drawLotto(){ //The validation I have here I know isn't the most effective way and is-
Random r = new Random();//resource comsuning but this was the only way I could think of doing it.
int draw[] = new int[6];
int min = 1;
for(int i = 0; i < draw.length; i++){
draw[i] = r.nextInt(lotteryMax-min) + min;
lotteryDraw.add(draw[i]);
}
int size = lotteryDraw.size();
if(size != 6){
drawLotto();
}
for(int i = 0; i < draw.length; i++){
System.out.println(draw[i] + " ,");
}
System.out.println();
}
``
Thank you, any help is appreciated.
The reason you have problems is because you recursively call drawLotto(), which will in turn create a new instance of the Random. If drawLotto() cannot create a correct list, it will have to do a full retry of all 6 numbers. This might cause your application to use a high amount of memory, resulting in the crash you experience
One way you could do this is by keep looping until you find 6 unique numbers:
public void drawLotto(){
Random r = new Random();
Set<Integer> draw = new HashSet<>();
int min = 1;
int lotteryMax = 50;
while(draw.size() < 6){
draw.add(r.nextInt(lotteryMax-min) + min);
}
String lotteryDrawing = draw.stream().map(String::valueOf).collect(Collectors.joining(" ,"));
System.out.println(lotteryDrawing);
}
Though you have to make sure that your lotteryMax is higher than the number you need
check this out
public void drawLotto(){
Random random = new Random();
while(lotteryDraw.size()<6) {
lotteryDraw.add(random.nextInt(max-min)+min);
}
lotteryDraw.forEach(System.out::println);
}
Use a set if you want to avoid duplicate values.
Example:
public static Set <Integer> drawLotto() { //The validation I have here I know isn't the most effective way and is-
Random r = new Random(); //resource comsuning but this was the only way I could think of doing it.
int draw[] = new int[6];
int min = 1;
int lotteryMax = 10;
Set<Integer> lotteryDraw = new HashSet<Integer>();
for (int i = 0; i < draw.length; i++) {
draw[i] = r.nextInt(lotteryMax - min) + min;
lotteryDraw.add(draw[i]);
}
int size = lotteryDraw.size();
if (size != 6) {
return drawLotto();
} else {
return lotteryDraw;
}
}
Related
I'm trying to get the secret code from 1 - 40, not to repeat any numbers. How am I able to compare each of them and not get any duplicates?
I have extensively looked through Java documentation and asked my lectures and I can't get a working answer. I understand the concept, what I'm meant to do, but just can't get the coding right.
Here is my code:
public static void main(String[] args) {
int[] secret = new int[5];
//int[] secret = {0,0,0,0,0};
int[][] num = new int[3][5];
int correctL1 = 0;
int correctL2 = 0;
int correctL3 = 0;
for (int i = 0; i<5; i++){ // to get secret numbers.
secret[i] = (int) ((Math.random() * (40 - 1)) + 1);
}
System.out.println(Arrays.toString(secret));
}
I have tried putting this into the loop to get another number but it's still giving me duplicates.
if(((secret[i] == secret[0]) || (secret[i] == secret[1]) ||
(secret[i] == secret[2]) || (secret[i] == secret[3]) ||
(secret[i] == secret[4])) || (secret[i] != 0)) {
secret[i] = ((int) ((Math.random() * (40 - 1)) + 1));
}
In Java 8, you can use Random#ints to easily generate an array of distinct random numbers:
int[] secret = new Random().ints(1, 40).distinct().limit(5).toArray();
Ideone Demo
Otherwise, you can generate a Set<Integer> by using a while loop:
Set<Integer> secret = new HashSet<>();
Random gen = new Random();
while (secret.size() < 5) {
secret.add(gen.nextInt(40 - 1) + 1);
}
Simply use a Set<Integer> to keep the already generated numbers and iterate as long as the generated number is part of the generated numbers.
Set<Integer> existing = new HashSet<>();
for (int i = 0; i<5; i++){ // to get secret numbers.
// Loop until the generated number is not part of the already generated numbers
int value;
do {
value = (int) ((Math.random() * (40 - 1)) + 1);
} while (!existing.add(value));
secret[i] = value;
}
Why not creating a Set<Integer> (so you will not get any duplicate) and iterate until its size is 5 ?
Just use Set Collection..for better performance you can use below code
Set<Integer> data=new HashSet<Integer>();
If you want to get all elements by ascending order,you can use TreeSet
OR Other Logic is Every collection has contains method which returns boolean for e.g.data.contains(o)
while(true){
int[] secret = new int[5];
for (int i = 0; i < 5; i++) { // to get secret numbers.
secret[i] = (int) ((Math.random() * (40 - 1)) + 1);
}
System.out.println("Array:" + Arrays.toString(secret));
Set<Integer> mySet = new HashSet<Integer>();
for (int i = 0; i < 5; i++) { // to get secret numbers.
mySet.add(secret[i]);
}
if (mySet.size() == secret.length) {
System.out.println("OK");
break;
} else {
System.out.println("Not ok");
}
}
This question already has answers here:
Java - generate Random range of specific numbers without duplication of those numbers - how to?
(6 answers)
Closed 9 years ago.
Here's the code I have that solves my problem. But it seems really brute forced. Is there any optimized/elegant way to write this?
System.out.println("\n\nPart II: Let' put in a list of 50 random numbers between 10 to 99. No Duplicates!");
Linkedlist l1 = new Linkedlist();
Random rand = new Random();
for(int i = 0; i < 50; i++){
int num = rand.nextInt(89) + 10;//range between 10 and 99.
while(true){
if(!l1.search(num)){
l1.add(num);
break;
}
else
num = rand.nextInt(89) + 10;//recycle for new num
}//infinite loop until new non-duplicate random value is generated for the list.
}//for
Well there is a more cleaner way to do this that doesn't involve randomizing so much and output rejection. You can populate a List with the numbers you require, in this case:
List<Integer> numberList = new ArrayList<>();
for(int i=11; i<=99; i++){
numberList.add(i);
}
Then shuffle the List and pick first N numbers from it..
Collections.shuffle(numberList);
for(int j=0; j<50; j++){
System.out.println(numberList.get(j));
}
You have to know your set in advance though to be able to populate it.
Bounded Fisher-Yates shuffle. Runtime for the random sampling is linear in the number of elements you need, not the number of elements you're picking from. You don't waste any time shuffling the entire range of elements or rejecting elements that have already been picked.
int[] sample(int sampleSize, int startInclusive, int endExclusive) {
int[] samples = new int[sampleSize];
int[] range = IntStream.range(startInclusive, endExclusive).toArray();
Random random = new Random();
for (int i = 0, j = range.length; i < samples.length; i++) {
int k = random.nextInt(j--);
samples[i] = range[k];
range[k] = range[j];
}
return samples;
}
I would prefer using Set instead of a List as Set will automatically handle duplicates so that we need to worry about eliminating them by our own.
Try this:
Set<Integer> set = new HashSet<Integer>();
Random rand = new Random();
while(true) {
int num = rand.nextInt(89) + 10;// range between 10 and 99.
set.add(num);
if (set.size() == 50) {
break;
}
}
System.out.println(set);
Sets don't allow duplicates:
Set<Integer> s = new HashSet<Integer>();
Random rand = new Random();
while (s.size() < 50) {
int num = rand.nextInt(89) + 10;// range between 10 and 99.
s.add(num);
}
You could just use a Set, which will only allow unique values to be stored in it, this way you could just keep looping while the number of elements is less than 50...
Set<Integer> nums = new HashSet<>(50);
while (nums.size() < 50) {
nums.add((int)(10 + (Math.random() * 89)));
}
for (Integer num : nums) {
System.out.println(num);
}
This is a variation on #AnkurShanbhag's answer (I don't like while (true) loops ;)), so if you like, shoot them credit ;)
package com.project.stackoverflow;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;
public class RandomGenerator {
private Scanner s;
public TreeSet<Integer> compute() {
TreeSet<Integer> generatedList = new TreeSet<Integer>();
s = new Scanner(System.in);
System.out.println("Enter the lower bound for checking random numbers:");
long lowBound = s.nextLong();
System.out.println("Enter the upper bound for checking random numbers:");
long topBound = s.nextLong();
Random randomNumbers = new Random();
for (int i = 0; i < topBound; i++) {
if (generatedList.size() == 5) {
break;
} else {
generatorFunc(lowBound, topBound, randomNumbers, generatedList);
}
}
return generatedList;
}
public void generatorFunc(long lowBound, long topBound, Random randomNumbers, TreeSet<Integer> generatedList) {
long limit = topBound - lowBound;
long part = (long) (limit * randomNumbers.nextDouble());
int randomNum = (int) (part + lowBound);
generatedList.add(randomNum);
}
public void printList() {
TreeSet<Integer> testListVals = compute();
System.out.println("New" + testListVals);
}
public static void main(String[] args) {
RandomGenerator obj = new RandomGenerator();
obj.printList();
}
}
This question already has answers here:
Java generating non-repeating random numbers
(12 answers)
Closed 9 years ago.
I am trying to write a guessing game program where a 4 digit number is randomly generated. The numbers need to be unique (as in they do not repeat at any time) I am fairly new to Java and I am having trouble displaying the numbers in an array. Also I can't figure out a way to check a number against the others more than once. EX: If random number A is the same as random number B it will make a new random number A. But I dont know how to check if the NEW random A is the same as number B without writing the same code over and over and over. (clearly some kind of loop but I have no idea which kind)
import java.util.Random;
public class Game {
public static void main(String[] args) {
// TODO Auto-generated method stub
int rand1 = 0;
int rand2 = 0;
int rand3 = 0;
int rand4 = 0;
int[] randArray = new int[]{rand1, rand2, rand3, rand4};
Random randy = new Random();
int a = randy.nextInt(9);
int b = randy.nextInt(9);
int c = randy.nextInt(9);
int d = randy.nextInt(9);
//how to check the variable more than one time?
a = rand1;
if (b == a) {
b = randy.nextInt(9);
}
else rand2 = b;
if (c == a || c == b) {
c = randy.nextInt(9);
}
else rand3 = c;
if (d == a || d == b || d == c) {
d = randy.nextInt(9);
}
else rand4 = d;
System.out.print(randArray); //prints gibberish
//prints the numbers fine
//System.out.print(rand1);
//System.out.print(rand2);
//System.out.print(rand3);
//System.out.print(rand4);
}
}
You might first add the random numbers to a java.util.HashSet and then convert it to an array. This way you get rid of all duplicates.
How about using an ArrayList instead?
The syntax is different, but then you can do your program in a looping fashion.
For example:
ArrayList<Integer> randNums = new ArrayList();
while(randNums.size() != 4) {
int a = randy.nextInt(9);
if(false == randNums.contains(a))
randNums.add(a);
}
Edit to add a side note: ArrayList has the prettier printing you are looking for as well.
If you want to change your current array type to Integer instead of an int then i suggest you to take one of the other answers. My first instinct was to show you how clean, readable and simple it will be if you used ArrayList<Integer> and its power and then convert it to Integer[] again no int[].
At the end i decided to wrote you an answer, that may not be the most elegant and defentily not the shortest one, but it will teach you how to think right before you could use tools that will take those element off (ArrayList and its powers as we said).
The algorithm is quite simple.
You create int-array at the n size you needed.
You iterate over it from 0 to n and with every iteration you:
A. Creating a do-while loop that will generate a random number from 0-9.
B. Generate a random temp number from 0-9.
C. Iterating over your current readArray to look-up if the generated number is inside, and if so it will flag it and stop the look-up process (because we found that we already have it).
D. Will check if the flag isExists set as true, if so, then will go into step B again otherwise will go to step 3.
If we reached to the end of look-up(for) without changing flag to true, than the temp(generated number) is not at our current array, and it will be safe to add it.
Will check if we reach to the end of the array or there are more array cell to fill. i < readArray.length.
Code:
Random randy = new Random();
int[] readArray = new int[4];
for (int i = 0; i < readArray.length; i++) {
int temp;
boolean isExists;
do {
isExists = false;
temp = randy.nextInt(10);
for (int j = 0; j < i; j++)
{
if (readArray[j] == temp)
{
isExists = true;
break;
}
}
} while (isExists);
readArray[i] = temp;
}
System.out.println(Arrays.toString(readArray));
If you're OK with storing in memory an 'int' array of 10000 entries:
public class YourClass
{
private static int final SIZE = 10000;
private int[] array = new int[SIZE];
private int currIteration = 0;
private Random random = new Random();
public YourClass()
{
for (int i=0; i<SIZE; i++)
array[i] = i;
}
public int getRandVal()
{
int index = random.nextInt(SIZE-currIteration);
int val = array[index];
array[index] = array[SIZE-currIteration-1];
array[SIZE-currIteration-1] = val;
if (++currIteration == SIZE)
currIteration = 0;
return val;
}
}
For generating random unique integers
Use a Set to create a collection of unique values. Otherwise, for each random number generated, iterate over the array to ensure it's unique before adding it.
Integer[] createGuesses(int numGuesses, int low, int high)
{
Set<Integer> guesses = new HashSet<>();
Random rand = new Random();
while(guesses.size() < numGuesses)
guesses.add(low + rand.nextInt(high - low));
return guesses.toArray(new Integer[numGuesses]);
}
This is assessed work so please don't give the answer, just advice!
I'm trying to get my program to return the strings pass, compensation pass or fail depending on the values inputted by the user. However, it's not returning the values and I'm receiving an error for 'weighting'. Earlier it was working, however not in a suitable way because it wouldn't always return the correct before results. I added the array because i think that's what is needed, but now I'm just getting an error. Cheers.
enter code here
public class MarkCalculator {
static int[] marks = new int[12];
static int[] weighing = new int[6];
// public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int weighting;
int coursework;
int exammark;
for (int i = 0; i < marks.length / 2; i++) {
System.out.println("Please enter course work weighting");
weighting = kb.nextInt();
weighing[i] = weighting;
}
for (int i = 0; i < marks.length / 2; i++) {
System.out.println("Please enter course work mark ");
coursework = kb.nextInt();
marks[i] = coursework;
}
for (int i = 0; i < marks.length / 2; i++) {
System.out.println("Please enter exam mark ");
exammark = kb.nextInt();
marks[i + 6] = exammark;
}
System.out.println("Calculating Marks");
MarkCalculator mc = new MarkCalculator();
String[] results = mc.computeMarks(marks, weighing);
for (String result : results) {
System.out.println("Results are " + result);
}
}
public String[] computeMarks(int[] marks, int[] weighing) {
int[] formula = new int[12];
String[] results = new String[weighing.length];
for (int i = 0; i < weighing.length; i++) {
int exam = marks[i];
int cw = marks[i+weighing.length];
int weight = weighing[i];
formula [i]= ((cw + weight) + (exam * (100 - weight)) / 100);
if ((formula[i]<=39) && (formula[i] > 35)) {
results[i] = "COMPENSATION PASS";}
else if (formula[i] >= 40) {
results[i] = "PASS";
}
else {
results[i] = "FAIL";
}
}
return results;
}
public static void computeResult (int[] coursework, int[] exammark)
{
computeResult(coursework,exammark);
}
}
Was posted as comment:
You could separate the marks into two arrays which will be easier to debug? Also it seems like you might be going over the array index for weightings on this line
for (int i = 0; i < marks.length;i++)
{
int exam = marks[i];
int cw = marks[i];
int weight = weighing[i]; // Error is here
//...
}
Because "weighing" has a range 0-5 and you are cycling through to 0-11 (with the marks array)
weighting and marks are different length arrays, yet you are doing the loop
for (int i = 0; i < marks.length; i++)
which will go out of bounds for weighting when i > 5.
It looks like you need to do something like this:
for (int i = 0; i < weighting.length; i++) {
int cw = marks[i];
int exam = marks[i+weighting.length];
int weight = weighing[i];
But that will depend on how you are storing the marks for the cw and exam in the marks array. I would recommend creating separate arrays for cw and exam as these are different items and will make things a lot easier to read and debug for yourself.
As you've asked for tips to improve your program as well, without specific code, then I would consider doing the following:
1) Have separate arrays for exam and cw marks. You're making it hard for yourself to debug your program by concatenating them together and this is also the source of your error.
2) Assuming that you always have the same number of exams as you do cw elements then I would consider having a class variable in MarkCalculator that stores the number of tests. Something like this:
private static int NUM_TESTS.
This way you can initialise arrays like this:
private static int[] examMarks = new int[NUM_TESTS]
and you can do the looping in computeMarks like this:
for (int i = 0; i < NUM_TESTS; i++)
This way if you decide you want more tests you only have to update the code in one place. It would also be easy to change your program so that the user could define how many tests should be calculated.
3) Where you have:
weighting = kb.nextInt();
weightings[i] = weighting;
replace it with:
weightings[i] = kb.nextInt();
as the variable weighting only seem to be used in this place and is therefore unnecessary. This will result in fewer operations the program has to perform and reduces the amount of code on the screen. In practice the compiler will likely remove this redundant variable, but it is good practice to think about how many operations you are performing and which of them aren't necessary.
4) It's better practice to explicitly set access modifiers on fields in a class. So you should have:
'private static int[] weightings = new int[NUM_TESTS];`
If you want to access it from another class you would then typically specify a getter method like so:
public int[] getWeightings() { return weightings; }
5) This is less important, but I would move main to the bottom of the class. In Java it's more typical to see the classes fields first, then the constructor, then public methods, then private methods and have the main at the bottom. In large projects it helps keeping to good style as it makes the code easier to read and understand.
These reference might help you learn more:
Java Coding Style Guide
Oracle Tutorial on access-modifiers
You will encounter an error as "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6"
This is because in method computeMarks. The length of marks (int[]) is 12.
and you just declare a variable with length 6 to handle:
int[] formula = new int[6];
when variable i in for loop reaches 6. The following code will throw out an error.
formula [i]= ((cw + weight) + (exam * (100 - weight)) / 100);
Have a try to declare it with length of 12.
int[] formula = new int[12];
Just paste code for method computeMarks.
public String[] computeMarks(int[] marks, int[] weighing) {
int[] formula = new int[12];
String[] results = new String[weighing.length];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < weighing.length; i++) {
sb.setLength(0);
int exam = marks[i];
int cw = marks[i + weighing.length];
int weight = weighing[i];
formula[i] = ((cw + weight) + (exam * (100 - weight)) / 100);
if ((formula[i] <= 39) && (formula[i] > 35)) {
sb.append("COMPENSATION PASS");
} else if (formula[i] >= 40) {
sb.append("PASS");
} else {
sb.append("FAIL");
}
sb.append(" cw mark is ").append(cw).append(" and exam mark is ")
.append(exam);
results[i] = sb.toString();
}
return results;
}
First of all, I'm a newbie Android App developer. In my App I need a class which generates random numbers in a given range avoiding repetition. I've searched much for this issue, but I haven't found any concrete solution for my case. Well, thought I found one which I slightly readapt, but it's not working fine. The code is the following one:
public class NoRepeatRandom {
private int[] number = null;
private int N = -1;
private int size = 0;
public NoRepeatRandom(int minVal, int maxVal)
{
N = (maxVal - minVal) + 1;
number = new int[N];
int n = minVal;
for(int i = 0; i < N; i++)
number[i] = n++;
size = N;
}
public void Reset() { size = N; }
// Returns -1 if none left
public int GetRandom()
{
if(size <= 0) return -1;
int index = size * (int)Math.random();
int randNum = number[index];
// Swap current value with current last, so we don't actually
// have to remove anything, and our list still contains everything
// if we want to reset
number[index] = number[size-1];
number[--size] = randNum;
return randNum;
}
}
When I call GetRandom() I don't achieve the expected result, because it always returns the minimum number of the range given. For example:
NoRepeatRandom nrr = new NoRepeatRandom(0, 10);
int yes = nrr.GetRandom();
//Here I create a Toast just to see the numbers that the method is returning.
Toast toast = Toast.makeText(MainActivity.this, Integer.toString(yes), Toast.LENGTH_SHORT);
toast.show();
And the result is: 0,0,0,0,0,0....
For range (5,10) the result is 5,5,5,5....
Does anybody knows what is wrong with the code? I would really appreciate any help!
Thank you in advance.
I guess this line is issue.
int index = size * (int) Math.random();
This evaluates to Zero always.
Edit:
Just consolidating comments from Jeroen Vannevel and OP.
Math.random() will return a value between 0.0 and 1.0, which when cast with (int) will always evaluates to ZERO. You ca use as below.
int index = (int) (size * Math.random());
You should try using Random class:
http://developer.android.com/reference/java/util/Random.html
Random random = new Random();
int randomNo = random.nextInt(10); // 0 to 9