pick a number in array and avoid to pick the same number? - java

I want to make a function that pick a randomly number in array and avoid to pick the same number in next time.
Here is my code (it work in sometime and mostly inf-loop)
please help me, Thank you.
private static int pick(int[] x) {
int upperbound = x[x.length-1];
int lowerbound = x[0];
int count=0;
int ranvalue;
int ranindex;
Random rand = new Random();
do{
ranindex = rand.nextInt(upperbound-lowerbound) + lowerbound;
count++;
}while(x[ranindex]==-1||count!=x.length-1);
ranvalue=x[ranindex];
x[ranindex]=-1;
return ranvalue;
}

If your array has size n, then you can get at most n different indexes. I advise the following :
Create an array with numbers from 0 to n-1.
Shuffle it.
At each step, take the next element from this array and use it as an offset for your source array.
You should also wrap this logic into a class like this :
public class Picker {
private int[] source;
private List<Integer> offsets;
private int currentIndex = 0;
public Picker(int[] source) {
this.source = source;
Integer[] indexes = new Integer[source.length];
for(int i=0;i<source.length;i++) {
indexes[i] = i;
}
this.offsets = Arrays.asList(indexes);
Collections.shuffle(this.offsets);
}
public Integer next() {
return source[offsets.get(currentIndex++)];
}
}
Example :
public static void main(String[] args) {
int[] source = {8,3,5,9};
Picker picker = new Picker(source);
for(int i = 0; i<4;i++) {
System.out.println(picker.next());
}
}
Output :
5
3
8
9
EDIT : Or even simpler :
Integer[] source = {8,3,5,9};
//Copy the source and shuffle it
List<Integer> dest = Arrays.asList(source);
Collections.shuffle(dest);
//Then display
for (int i = 0;i<source.length;i++) {
System.out.println(dest.get(i));
}

Related

Modify the java program so that it works for the numbers in range between -25 and 25

I've started learning java some time ago. I'm reading through the Java Foundations book and doing exercises from the book to practice.
Just come across this one "Modify the java program so that it works for the numbers in the range between -25 and 25." and I wonder if you have any different solutions to it or is it really that simple? :)
Here's the original code:
public class BasicArray
{
public static void main(String[] args)
{
final int LIMIT = 15;
final int MULTIPLE = 10;
int[] list = new int[LIMIT];
// Initialize the array values
for(int index = 0; index < LIMIT; index++)
list[index] = index * MULTIPLE;
list[5] = 999; // change one array value
// Print the array values
for(int value : list)
System.out.println(value + "");
}
}
And here's my solution to it:
public class BasicArray
{
public static void main(String[] args)
{
final int LIMIT = 51;
final int MULTIPLE = 1;
int[] list = new int[LIMIT];
// Initialize the array values
for(int index = 0; index < LIMIT; index++)
list[index] = (index - 25) * MULTIPLE;
list[5] = 999; // change one array value
// Print the array values
for(int value : list)
System.out.println(value + "");
}
}
Yes, basically it's really simple exercise.
Regarding to your solution we actually don't need MULTIPLE in code.
public class BasicArray {
public static void main(String[] args) {
final int LIMIT = 51;
int[] list = new int[LIMIT];
// Initialize the array values
for(int index = 0; index < LIMIT; index++) {
list[index] = (index - 25);
}
list[5] = 999; // change one array value
// Print the array values
for(int value : list) {
System.out.println(value + "");
}
}
}
If you are ready for a bit of advanced java, you can try following:
public class BasicArray {
public static void main(String[] args) {
IntStream.rangeClosed(-25, 25)
.forEach(System.out::println);
}
}
Or this if you need to replace one value:
public class BasicArray {
public static void main(String[] args) {
IntStream.rangeClosed(-25, 25)
.forEach(i -> {
if (i == -20) { // change one array value
System.out.println(999);
} else {
System.out.println(i);
}
});
}
}

Missing Return Statement in Java Recursive Function

I'm trying to generate a list of 25 non-repeating random numbers in Java, and I keep getting the Missing Return Statement error. As can be seen, I tried putting return before calling the method within itself. Not sure what's missing. It also didn't work with just the return (rando)
import java.util.*;
public class arrayList{
ArrayList<Integer> checkRandom;
ArrayList<Integer> array4;
ArrayList<Integer> array2;
ArrayList<Integer> array3;
public int addRandom(){
Random rnd = new Random();
int b=0;
for (int i=0; i<26; i++){
int rando = rnd.nextInt(101);
if (checkRandom.indexOf(rando) != -1){
return addRandom();
}
else{
checkRandom.add(rando);
array4.add(rando);
return (rando);
}
}
for (int j=0;j<26;j++){
int right;
right = checkRandom.get(j);
System.out.println(right);
}
return -1;
}
public static void main(String args[]){
arrayList randomGen = new arrayList();
randomGen.addRandom();
}
}
Exception in thread "main" java.lang.NullPointerException
at arrayList.addRandom(arrayList.java:14)
at arrayList.main(arrayList.java:37)
I'd suggest you use a much simpler method using Java 8 streams. For example, to create an array of 26 distinct random integers betweeen 0 and 100:
int[] randomArray = new Random().ints(0, 101).distinct().limit(26).toArray();
To explain in a bit more detail, this statement can be interpreted as: create a random number generator, use it to generate an endless stream of random numbers between 0 and 100, remove any duplicates, get the first 26 numbers in the stream and convert them to an int array.
Streams are incredibly powerful. Once your generator is in this form it's trivial to add a sorted operator or a filter, or to collect them into a List or Map.
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Random rand = new Random();
while (list.size() < 25) {
int index = rand.nextInt(101);
if (!list.contains(index)) {
list.add(index);
}
}
System.out.println(list);
}
}
Initialize a method local variable b inside your addRandom method and reassign it in your for loop finally return variable b.
public int addRandom(){
Random rnd = new Random();
int b=0;
for (int i=0; i<26; i++){
int rando = rnd.nextInt(101);
if (checkRandom.indexOf(rando) != -1){
b= addRandom();
}
else{
checkRandom.add(rando);
array4.add(rando);
b=rando;
}
}
return b;
}
Your method
public int addRandom(){
Random rnd = new Random();
for (int i=0; i<26; i++){
int rando = rnd.nextInt(101);
if (checkRandom.indexOf(rando) != -1){
return addRandom();
}
else{
checkRandom.add(rando);
array4.add(rando);
return (rando);
}
}
}
does not have a return statement at the end. The method signature states you must return an integer. The compiler does not know that the for statement will be executed until runtime. Thus, you have to handle the case where the for loop is not executed. Since you can tell it will be executed every time, adding a return -1; before the end of the method will solve your problem.
i.e.
public int addRandom(){
Random rnd = new Random();
for (int i=0; i<26; i++){
int rando = rnd.nextInt(101);
if (checkRandom.indexOf(rando) != -1){
return addRandom();
}
else{
checkRandom.add(rando);
array4.add(rando);
return (rando);
}
}
return -1;
}
You can call the method by creating an instance of the class i.e.
arrayList randomGen = new arrayList();
randomGen.addRandom();
Btw, its standard in java to name your classes CamelCased. i.e. ArrayList. Although, you may want to rename it something else so you don't confuse your class with java.util.ArrayList (a popular java class)
If you want use recursion, you don't need loops. for example:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test {
List<Integer> randomList = new ArrayList<Integer>();
Random rnd = new Random(); // do not create new Random object in each function call.
final static int LIST_SIZE = 25;
public void addRandom(List someList) {
if (randomList.size() < LIST_SIZE) {
int random = rnd.nextInt(101); // LIST_SIZE must be lesser than 101 otherwise you will got infinite recursion.
if (!randomList.contains(random)) {
randomList.add(random);
someList.add(random);
}
addRandom(someList);
}
}
public static void main(String args[]) {
Test test = new Test();
List<Integer> array4 = new ArrayList<Integer>();
test.addRandom(array4);
for (Integer value : array4) {
System.out.println(value);
}
}
}

Random Data Analyzer

I'm creating a program that will generate 100 random numbers between 1 and 1000, add them to a list, and then sum up those numbers. Here's my code:
public class Iteration {
public static void main (String [] args){
private int RandomDataAnalyzer(int Rando) {
Random rand = new Random();
List<Integer> NumList = new ArrayList<Integer>();
for (int i=0;i<=100;i++){
Rando = rand.nextInt(1001);
NumList.add(Rando);
}
int sum = 0;
for (int i=0; i<100; i++)
{
Rando = rand.nextInt(100);
sum = sum + Rando;
}
return sum;
}
}
}
And here's my errors:
H:\Java\Iteration.java:12: error: illegal start of expression
private int RandomDataAnalyzer(int Rando) {
^
H:\Java\Iteration.java:12: error: ';' expected
private int RandomDataAnalyzer(int Rando) {
^
H:\Java\Iteration.java:12: error: ';' expected
private int RandomDataAnalyzer(int Rando) {
Any help, please?
You can't define a method inside another method. Close your main method first, then start RandomDataAnalyzer:
public static void main(String[] args) {
// Contents of main.
}
public int RandomDataAnalyzer(int Rando) {
// Contents of RandomDataAnalyzer.
}
I'm going to assume that this isn't a homework problem and that you're learning Java on your own. If I'm wrong, shame on me for giving a working version. But my impression is that you'll learn from this:
public class gpaCalc
{
static Random rand;
static int rando;
public static void main(String[] args)
{
ArrayList<Integer> NumList = new ArrayList<>(); // use ArrayList
for (int i=0;i<=100;i++)
{
rand = new Random();
rando = rand.nextInt(1001);
NumList.add(rando);
}
int sum = 0;
for (int i=0; i<100; i++)
{
rando = NumList.get(i); // get is "opposite" of put--get the value put into the list earlier
sum = sum + rando;
}
System.out.println(sum);
}
}
There doesn't seem to be a need for a separate method called randomDataAnalyzer.
Welcome to StackOverflow.
Let's begin with the first issue: randomDataAnalyzer is being defined inside main method. Which is wrong, you should be actually defining it at the same level.
Taken into consideration, you should also add the static word before this function because this method is part of the class, and not of the elements. It's not necessary to create a new element of the class 'Iteration' for using a simple method.
Last, but not least, you are looping through the arraylist incorrectly. You are not even calling it. As you will see now:
import java.util.*;
public class Iteration
{
public static void main(String[] args)
{
ArrayList<int> numberList = new ArrayList<int>(); // we define the arraylist
for (int i = 0; i < 100; i++)
{
numberList.add(new Random().nextInt(1001)); // we add a random number to the list
}
// finally, after the 100 values were added..
System.out.println(randomDataAnalyzer(numberList)); // we show the output
}
public static int randomDataAnalyzer(ArrayList<int> list) // we will send this function an arraylist which will get the Σ.
{
int sum = 0;
for (int value : list) // this is how we loop through foreach value in the list
{
sum += value; // which means sum = sum + value.
}
return sum; // after looping and summing up, here'll be the result
}
}
I hope this was what you were looking for.
Here's a working version. Note the changes to your original:
Don't define methods inside methods: it is illegal syntax.
Rando, NumList: the naming conventions are to start classes, interfaces, enums (i.e. types) with a capital letter, and methods, fields, and variables with a lowercase case letter;
Removed the int Rando parameter alltogether: it's value was never used (it was only assigned to)
Use the values from numList rather than generating new numbers.
Added a method illustrating that the use of such a list is not needed in this case; the 'list' of 1000 numbers is still present, but only conceptually.
import java.util.*;
public class Iteration {
public static void main (String [] args) {
int sum = new Iteration().randomDataAnalyzer();
System.out.println(sum);
}
private int randomDataAnalyzer() {
Random rand = new Random();
List<Integer> numList = new ArrayList<Integer>();
for ( int i=0; i<100; i++ )
{
numList.add( 1 + rand.nextInt(1000) );
}
int sum = 0;
for ( int i=0; i<numList.size(); i++ )
{
sum = sum + numList.get(i);
}
return sum;
}
// Note that the above method has the same effect as this one:
private int moreEfficient() {
Random rand = new Random();
int sum = 0;
for ( int i=0; i < 100; i++)
sum += 1 + rand.nextInt(1000);
return sum;
}
}

Counting the number of an element in an array?

So for an assignment in my class, we have a lab where we have to write code that will do three things:
generate an array of 50 random numbers from 0-9.
Return how many 8's appear in the array.
Return the number of runs in the array.
We were given a .java file to start out with that is this:
package ArrayOfInts;
public class ArrayOfInts {
private int [] intArray;
private int eights;
private int runs;
//Sets intArray to the parameter and initializes all other variables
public ArrayOfInts(int [] x){
}
//Returns how many 8's are in the array.
public int findTheEights(){
return eights;
}
//Returns the number of runs in the array.
public int countTheRuns(){
return runs;
}
}
The code I've written so far is this:
package ArrayOfInts;
public class ArrayOfIntsTester {
public static void main(String [] args){
int [] testArray = new int [50];
for(int i = 0; i < testArray.length; i++){
int x = (int)(Math.random()*9);
testArray[i]= x;
System.out.print(x + ", ");
}
System.out.println();
ArrayOfInts test = new ArrayOfInts(testArray);
System.out.println(test.findTheEights());
System.out.println(test.countTheRuns());
}
}
I honestly have no idea where to start with this. Help?? The code I've written generates the correct type of array, but I don't know how to count what I need to count for it.
For the eights: iterate over the array and do eights++ everytime you find an eight.
if (testArray[i] == 8){
eights++
}
Here is some pseudo code to help you count the eights:
numberOfEights = 0;
for each element in testArray{
if (element equals 8) {
numberOfEights = nnumberOfEight + 1
} else {
nothing to do
}
}
return numberOfEights
Try to turn this to java code.
For the other part, i don't understand what is your runs.
//To count number of eights, iterate through array with a counter and increment it when we see an 8
public ArrayOfInts(int [] x){
eights = 0; //reset our count
for (int currentInt : intArray)
{
if (currentInt == 8)
eights++;
}
//Process runs here
}
To count the number of eight just use another variable which is initialized to 0 , like this :-
int c=0;
for (i=0;i<testArray.length;i++)
{
if (testArray[i] == 8)
{
c++;
}
}
Now your c has the number of 8's present in your array you cant print its value.
--> If that's what you are trying to ask
first initialize the array in constructor
public ArrayOfInts(int [] x){
intArray=x;
}
initialize the eights to 0
private int eights=0;
then update your count method
public int findTheEights(){
for(int current:intArray)
{
if(current==8){ eights++;}
}
return eights;
}
In JavaScript you could do it like the following example.
Counting of eights can be done directly at the initializaion of the array.
And if I understand you right, you'd like to count the instances of the object with the countTheRuns(). That can be done with a class variable.
If you'd like to play with the code, you'll find it here at jsFiddle.
/*So for an assignment in my class, we have a lab where we have to write code that will do three things:
generate an array of 50 random numbers from 0-9.
Return how many 8's appear in the array.
Return the number of runs in the array.
*/
(function() {
var MAX_RANDOM = 50;
var NUM_RANGE = 9; // 0 to 9
function ArrayOfInts() {
this.eights = 0;
this.numbers = [];
this.init();
// class variabe run_counter
ArrayOfInts.run_counter = (ArrayOfInts.run_counter || 0) +1;
this.getNumbers = function() {
return this.numbers;
};
this.getEights = function() {
return this.eights;
};
}
ArrayOfInts.prototype.init = function() {
for (var i=0; i< MAX_RANDOM; i++) {
var randNum = Math.floor(Math.random()*(NUM_RANGE+1));
this.numbers.push(randNum);
if (randNum == 8) this.eights++;
}
};
// demo usage code ---------------------
var array = new ArrayOfInts();
var array2 = new ArrayOfInts();
document.getElementById('output').innerHTML = array.getNumbers();
console.log(array.getNumbers());
document.getElementById('output').innerHTML += '<br/>The array has the following no. of eights: ' + array.getEights();
document.getElementById('output').innerHTML += '<br/>The number generation ran ' + ArrayOfInts.run_counter + ' times.';
})();
<div id="output"></div>

how to get random item from an arraylist without any repeatation in android

I have an ArrayList of an object from where I want items of a particular position, but everytime I launch the Activity the retrieved position should be randomize and also won't repeat until every position Item is completely retrieved. I used this method:
public static int getRandomNumber(ArrayList<Integer> arr)
throws IllegalArgumentException {
try {
Random random = new Random();
int select = random.nextInt(arr.size());
int randomnum = arr.get(select);
GlobalData.randList.remove(select);
return randomnum;
} catch (IllegalArgumentException e) {
for (int i = 0; i < arr.size(); i++) {
GlobalData.randList.add(i);
}
return 0;
}
but its not working,like duplicate number is coming, there may be a reason because everytime I am re launching the activity. I did it in oncreate instead of onResume but its not working as I expected? Is there any other way to work with it? Any solution?
Use Collections.shuffle() to shuffle the array. Use another variable to track the current position in the array. Each time you retrieve a new value increment the variable. Once you reach the end of the array re-shuffle it.
Reference:
Shuffling algorithms
public class RandomArray {
ArrayList<Integer> array = null;
int position = 0;
public RandomArray(ArrayList<Integer> arr) {
array = arr;
position = arr.size();
}
public int getNext() {
if (position == array.size()) {
position = 0;
Collections.shuffle(array);
}
return array.get(position++);
}
}
If you don't care about the original order, you can try this:
Object[] array = new Object[10]; // say 10 objects
int remain = array.length;
Random rnd = new Random();
public Object next () {
if (remain == 0) {
return null;
} else {
int i = rnd.nextInt(remain--);
Object tmp = array[i];
array[i] = array[remain];
array[remain] = tmp;
return tmp;
}
}
You can also do similar thing with ArrayList.
Well, in this way, it is faster than shuffle() method. shuffle() has the time complexity of O(n) while my code is O(1).

Categories

Resources