adding items to arrays alternatively - java

i want a program that can add the generated random numbers(unique not repeating) to 2 arrays alternatively...
this is what i have done so far:
int n = 56,m=0;
int e=0,f=0;
int[] deck1 = new int[n];
int[] deck2 = new int[m];
Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
for(int i=1;i<=53;i++)
{
while(true)
{
next = rng.nextInt(53) + 1;
if (!generated.contains(next))
{
generated.add(next);
break;
}
}
for(int t = 2;t<=i;t++)
if(t%2==0)
{deck1[e]=next;e++;}
else
{deck2[f]=next;f++;}
}
but i am not getting the result. please help.
thanks.

You can fix your immediate problem by forgetting about the inner for loop (iterating over t) and instead compute using i:
if ( i % 2 == 0 ) {
deck1[e] = next;
e++;
} else {
//...
The problem is that currently at each iteration of the loop, you add the element to each array many times (up to i times).
Some other suggestions:
Make generated a Set<Integer> (HashSet<Integer>), it is much better suited to this task
A better algorithm would be to construct a List<Integer> containing the numbers 1 through 53 and shuffle (e.g. with Collections.shuffle()). Then you could just add the first half of the list to deck1 and the second half to deck2.

Related

Counting cycles in a permutation of an array

I have to write some code to count cycles in a permutation of an array.
I've found on Google what I think is a solution, written in python:
def key(element):
return element
def find_cycles(l):
seen = set()
cycles = []
for i in range(len(l)):
if i != key(l[i]) and not i in seen:
cycle = []
n = i
while 1:
cycle.append(n)
n = key(l[n])
if n == i:
break
seen = seen.union(set(cycle))
cycles.append(list(reversed(cycle)))
return cycles
So far I wrote this code myself, which is my attempt to convert the Python code into Java code and it doesn't work:
public static ArrayList<Integer> numbers;
public static ArrayList<Integer> cycles;
public static ArrayList<Integer> cycle;
public Puslespil(int permutations){
numbers = new ArrayList<Integer>();
for (int i = 0; i<permutations;i++){
numbers.add(i);
}
}
public static void main(String[] args){
new Puslespil(5);
ArrayList<Integer> visited = new ArrayList<Integer>();
cycles = new ArrayList<Integer>();
Collections.shuffle(numbers);
System.out.println(numbers);
for(int i=0; i<numbers.size();i++){
if(i != numbers.get(i) && !visited.contains(i)){
cycle = new ArrayList<Integer>();
}
int n = i;
while(true){
cycle.add(n);
n = numbers.get(n);
if(n == i)
break;
visited.addAll(cycle);
Collections.reverse(cycle);
cycles.addAll(cycle);
}
}
System.out.println("cycles: " + cycles);
}
The Python code I found returns the numbers involved in each cycle, but I really only need to find the amount of cycles.
How can I convert the code properly, or come up with another solution?
I think you want to find the cycles of a permutation stored in an array (that is, the values of the array are the indices 0..N-1).
Right?
If that is the case, all you need to do is convert from an array representation of the permutation to a graph/adjacency matrix representation and then run a connected component algorithm as described in this answer (also python code).
The number of connected components is the number of cycles.

Randomly prints elements in an array

Is there a way where you can use Math.random to prints the element in a given array?
int[] list = new int[] {1,2,3};
So the output will be like
2,1,3
or
3,1,2
or
2,3,1
Perhaps you can approach it by shuffling your array then print it. If the original should not be modified, you can make a copy and then shuffle the copy.
There are well-known algorithms for shuffling array (or a deck of cards). One can be found here. An implementation in java looks like this:
static void shuffleArray(int []array) {
int length = array.length;
for (int i = length -1; i > 0; i--) {
// generate a random 0 <= j < i
int j = (int)(Math.random() * i);
// swap elements at i and j
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
The approach proposed in most answers is extremely inefficient, as it works in O(N2) time. Think about it: at first you'll generate unused indexes with one attempt, but closer to the end, when almost all array is processed, it will require nearly N steps to generate next unused index.
The optimal O(N) approach is to create shuffled array of indexes (0..N) where each index appears only once and then process your original array in the order of shuffled indexes. Each step requires O(N) time, so the whole algorithm is O(N).
int[] input = new int[]{5, 4, 3, 6, 2, 1};
int []indices = new int[input.length];
//Fisher-Yates shuffle
Random rnd = new Random();
for (int i = 0; i < indices.length; i++) {
int j = rnd.nextInt(i + 1);
indices[i] = indices[j];
indices[j] = i;
}
for (int i : indices) {
System.out.println(input[i]);
}
I didn't use Collections.shuffle, as it would require usage of Collection and thus wrapped Integer elements, which is very inefficient comparing to the plain int array.
Also, if you are ok with modifying your original array, you can just shuffle it in place (using the same Fisher-Yates shuffle) and then consume it while traversing.
UPD: Replaced shuffling array of indices with shuffled initialization.
Since you have java 8, you can take advantage of the beautiful Stream API.
In short, you can do:
new Random().ints(1, 500).limit(500).forEach(p -> System.out.println(list[p]));
Where 1 is the lowest int generated (inclusive) and 500 is the highest (exclusive). limit means that your stream will have a length of 500, maybe in that argument you want to put list.length.
For your case:
int[] list = new int[] {1,2,3,4,5,6};
new Random().ints(0, list.length).limit(10).forEach(p -> System.out.println(list[p]));
Prints: 5 2 5 4 6 3 3 5 6 4 (Obviously will not print the same numbers for you)
Create a random integer that may be as high as the length of the array - 1. If the random integer is equal to a previous used random integer -- known by storing used integers in an array -- create a new random integer. Otherwise, print the string correlated with that index specified by the random integer. If the length of the array storing the used random integers is equal to the length of the array of strings, stop the process.
This should print all your strings only once each and randomly.
Here is the solution
public static void main(String[] args) {
int[] list = new int[] { 1, 2, 3 };
int[] aux = new int[list.length];
int countTimes = 0;
while (countTimes < list.length) {
int position = new Random().nextInt(list.length);
if (aux[position] != list[position]) {
System.out.println(list[position]);
aux[position] = list[position];
countTimes++;
}
}
}
As I said in the comments. This answer will work.. All you need to do is track the indices that it accessed so you don't repeat them or remove that element from the array.
void printRandom(int[] array) {
if (array.length == 0)
return;
Random rand = new Random();
int rnd = rand.nextInt(array.length);
int element = array[rnd];
array = ArrayUtils.removeElement(array, element);
System.out.print(element);
printRandom(array);
}
Just repeat this process until all elements are removed. Obviously add checks to prevent errors and keep in mind I haven't used JAVA in a long time so post back if you have issues!
Lastly keep in mind this deletes the array so you may want to wrap this code in a function and then copy the array as a local variable so you can reuse the original as needed
In this case we can print random value from array using like below :
Steps:
Create list object of Integer to hold printed indices
Get random number and check whether this index is already printed or not
if not printed then add it in list and print value from array using this index
if list size and array length is equal then terminate the loop
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RandomIndices {
public static void main(String[] args) {
int[] list = new int[]{1, 2, 3};
Random random = new Random();
List<Integer> randomIndices = new ArrayList<>(); //to hold indices which are already printed
boolean isRemain = true;
while (isRemain) {
int randomIndex = random.nextInt(list.length);
if (!randomIndices.contains(randomIndex)) { //check random index value of array is printed or not
randomIndices.add(randomIndex);
System.out.println(list[randomIndex]);
}
if (randomIndices.size() == list.length) {
isRemain = false;
}
}}
}
Implement a simple "do while" statement to prevent duplicate numbers from showing up out of your array (I used a StringArray - but an IntegerArray would work the same way - as a side note, I can place the complete code up here but didn't want to do so if it didn't apply. I use a drop-down to select how many random words to generate - then display that set of true RANDOM words (non-repeated):
final Random rand1 = new Random();
final Random rand2 = new Random();
final int rndInt1 = rand1.nextInt(getResources().getStringArray(R.array.words).length);
int rndInt2 = rand2.nextInt(getResources().getStringArray(R.array.words).length);
if (rndInt1 == rndInt2){
do {
rndInt2 = rand2.nextInt(getResources().getStringArray(R.array.words).length);
}while (rndInt1 == rndInt2);//if indexes are equal - re-run the array search
}
outString = getResources().getStringArray(R.array.words)[rndInt1];
outString += ", " + getResources().getStringArray(R.array.words)[rndInt2];//concatenate the list
textWord = (TextView) findViewById(R.id.textWords);//An empty text field in my layout
textWord.setText(outString);//Set that empty text field to this string of random array elements

make an array with 10 unique numbers in java

it's my first time I ask here question.
I wanna make ArrayList with 10 unique int numbers from 0 to 9.
I do next steps:
Create empty Arraylist
Add first random number, so I can check for repetition later
Next I create new random int value, check if I've already have this one in ArrayList. If I have - I try another number, if I have not - I add this number.
If I got 10 numbers, I stop the loop
My code:
public static void main(String[] args) {
Random rd = new Random();
ArrayList<Integer> list = new ArrayList<Integer>();
int q = rd.nextInt(10);
list.add(q);
while (true) {
int a = rd.nextInt(10);
for (int b=0;b<list.size();b++){
if (a == list.get(b)) break;
else list.add(a);
}
if (list.size() == 10) break;
}
System.out.println(list);
}
But all I see in console is endless process.
The question is - is there another way to make ArrayList with 10 unique numbers (0 to 9)?
Use Collections.shuffle after initializing your ArrayList with the numbers.
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
{
list.add(i);
}
Collections.shuffle(list);
That will run in linear time because ArrayList is RandomAccess.
Using Java 8 Streams
List<Integer> shuffled =
// give me all the numbers from 0 to N
IntStream.range(0, N).boxed()
// arrange then by a random key
.groupBy(i -> Math.random(), toList())
// turns all the values into a single list
.values().flatMap(List::stream).collect(toList());

Randomly choose int number

I do have the following code for taking random int numbers
for (int i=1;i<=5;i++) {
int rand= new Random().nextInt(10);
Log.d("Ramdom number", String.valueOf(rand));
}
The problem is that I dont want random numbers to be repeated, mean when I run this code it gives to me 5 numbers but two of them at least repeats. Any advice?
For a small range of numbers to choose from, this should do the trick:
ArrayList<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < 20; ++i) {
numbers.add(i);
}
Collections.shuffle(numbers);
for (int i = 0; i < 5; ++i) {
Log.d("Random number", numbers.get(i).toString());
}
The problem is that you are creating a Random object within the loop. If the loop is a 'tight', as in this case, the Random object will be seeded with the same value. Moving the Random object initialization outside the loop should do the trick.
Random r = new Random();
for (int i=1;i<=5;i++) {
int rand= r.nextInt(10)
Log.d("Ramdom number", String.valueOf(rand));
}
EDIT:
This should work (at least it did for me)
public static Integer[] getRangedInt(int maxRange, int numCount)
{
if (maxRange < numCount)
{
throw new Exception("maxRange cannot be smaller than numCount");
}
Set<Integer set = new HashSet<Integer>();
Random r = new Random();
while (Set.size() < numCount)
{
int random = r.nextInt(maxRange);
while (!set.add(random))
{
random = r.nextInt(maxRange);
}
}
return set.toArray(new Integer[set.size()]);
}
final int maxnumbers = 5;
final int maxvalue = 10;
final Random generator = new Random();
Set<Integer> numbers = new HashSet<Integer>();
while(numbers.size() < maxnumbers){
numbers.add(random.nextInt(maxvalue));
}
After this loop you should have maxnumber non-repeating random numbers between 0 and maxvalue in the set numbers. You have to watch out so you don't get too many iterations when using this method, i.e. generating 9999 non-repeating numbers out of 10000 would probably take a long time.
Another more scalable version would be to have a list of numbers:
List<Integer> numbers = new ArrayList<Integer>();
for(int i = 0; i<maxvalue; i++){ numbers.add(i); }
Collections.shuffle(numbers);
List<Integer> randomnums = numbers.subList(0, maxnumbers);
I think you need a SET of random numbers. This hint should suffice.
If not, then please comment.
You can maintain one list of generated numbers
boolean flag=false;
Vector<int> vec = new Vector<int>();
for (int i=1;i<=5;i++) {
flag=false;
int rand= r.nextInt(10);
for(int j=0;j<vec.size();j++)
{
if(vec.get(j)==rand)
{
flag=true;
break;
}
}
if(flag)
{
continue;
}
else
{
Log.d("Ramdom number", String.valueOf(rand));
vec.add(rand);
}
}
You can maintain a vector of generated numbers and check
Is that number already generated then generate new one
else display this number
So what you're looking for isn't a list of random numbers, it's a list of 30 numbers randomly ordered.
One way is to generate a list of all possible values, then order them randomly, then peel them from the front of the list as needed. Here's some pseudocode:
for(int i=1; i<=30; i++) {
double r = rand();
while (null != aTreeSet.get(r)) r = rand();
aTreeSet.put(r, i);
}
where rand() returns some random value (not the 1-30 you seek, that's i) perhaps between 0 and 1 and aTreeSet is what you think.
The loop prevents sadness in the unlikely event of a dup being returned by rand().
To use this, pull values from aTreeSet in sorted order.
edit - Gross solution
Another way is to generate the 1-30 value, and if it isn't already in a "I have seen this" Set, add it and return the value. if it is there, generate a new random number. Repeat until an unused number is discovered. This performs poorly, relatively speaking, for the last few values. For 30 values on modern processors, it will get done in milliseconds, of course. If your max value was 1,000 instead of 30, I'd start getting concerned.
What you want is a random combination, use a Hash table to avoid repetitions
From the top of my head the code should be something like:
Ramdom r = new Random();
Hashtable<Integer, Integer> h = new Hashtable<Integer, Integer>();
while( h.keys().size() < 5 ) {
int i = r.nextInt(10);
h.put(i,i);
}
Integer[] k = (Integer[]) h.keySet().toArray();
The line
h.put(i,i);
just overrides the value if it is repeated, so only different drawn numbers will have entries in the hash table.
You can save your generated numbers in a Set and use the random number only if it is not in the set
Random r = new Random();
Set<Integer> generatedNumbers = new HashSet<Integer>();
for(int i = 1;i<=5;i++) {
int rand = r.nextInt(10)
if (!generatedNumbers.contains(rand)) {
Log.d("Ramdom number", String.valueOf(rand));
generatedNumbers.add(rand);
}
}

Generate k distinct numbers less than n

The task is this: generate k distinct positive numbers less than n without duplication.
My method is the following.
First create array size of k where we should write these numbers:
int a[] = new int[k];
//Now I am going to create another array where I check if (at given number
//position is 1 then generate number again, else put this number in an
//array and continue cycle.
I put a piece here of code and explanations.
int a[]=new int[k];
int t[]=new int[n+1];
Random r=new Random();
for (int i==0;i<t.length;i++){
t[i]=0;//initialize it to zero
}
int m=0;//initialize it also
for (int i=0;i<a.length;i++){
m=r.nextInt(n);//random element between 0 and n
if (t[m]==1){
//I have problems with this. I want in case of duplication element
//occurs repeat this steps afain until there will be different number.
else{
t[m]=1;
x[i]=m;
}
}
So I fill concret my problem: if t[m]==1. It means that this element occurs already so I want to
generate a new number, but problem is that number of generated numbers will
not be k because if i==0 and occurs duplicate element and we write continue then it will switch at i==1.
I need like goto for the repeat step. Or:
for (int i=0;i<x.length;i++){
loop:
m=r.nextInt(n);
if ( x[m]==1){
continue loop;
}
else{
x[m]=1;
a[i]=m;
continue; //Continue next step at i=1 and so on.
}
}
I need this code in Java.
It seems that you need a random sampling algorithm. You want to be able to choose m random items from the set {0,1,2,3...,n-1}.
See this post, where I wrote about 5 efficient algorithms for random sampling.
Following is Floyd's implementation, which can be useful in your case:
private static Random rnd = new Random();
...
public static Set<Integer> randomSample(int n, int m){
HashSet<Integer> res = new HashSet<Integer>(m);
for(int i = n - m; i < n; i++){
int item = rnd.nextInt(i + 1);
if (res.contains(item))
res.add(i);
else
res.add(item);
}
return res;
}
Create an array arr with n elements (1..n);
for i=1 to k {
result[i] = arr[rand]
delete arr[result[1]]
}

Categories

Resources