Gerator Image Random - android

I have is how to generate random images without repeating , as an example driving in an array 15 images and 7 images to send imageview but that are not repeated .
int[] img={ R.drawable.ima1,R.drawable.ima2,R.drawable.ima3,R.drawable.ima4,R.drawable.ima5,R.drawable.ima6,R.drawable.ima7,R.drawable.ima8,R.drawable.ima9,R.drawable.ima10,R.drawable.ima11,R.drawable.ima12,R.drawable.ima13,R.drawable.ima14,R.drawable.ima15};
int [] game= new int[7];
Random numerRan = new Random();
for (int i=0;i<game.length ;i++)
{
int num= numerRan.nextInt(17);
int x= img[num];
game[i]=img[num];
}
img1.setImageResource(juego[0]);
img2.setImageResource(juego[1]);
img3.setImageResource(juego[2]);
img4.setImageResource(juego[3]);
img5.setImageResource(juego[4]);
img6.setImageResource(juego[5]);
img7.setImageResource(juego[6]);
img8.setImageResource(juego[7]);
When I press the button control images to the score rando the position that was generated . The problem I have is that images are repeated.
I am using 2 arrangements the first is to save all the images and the second keep the random images that will assign imageview .

Instead of using an int[] array, use a List<Integer> instead, such as an ArrayList<Integer>.
That way, you'll be able to use Collections#shuffle(), which will have the desired effect. It'll randomly permute your list, in a non-repeating way. All you'll have to do later is to iterate on that List.
For more info, please refer to the Collections documentation.
You can also convert that array to a List, like the following:
List<Integer> myList = Arrays.asList(img);
For more info about that, take a look at this question.

Related

How to get strings from array randomly without repeating the string?

I'm making a quiz app I have string array and I want to load them on button click, strings should be loaded randomly into Text View. Strings should not be repeated.
Thanks..
Because it's your quiz, I'm gonna give you hints not the whole soltion
Random rnd = new Random;
while(array.size()>0){
// 1. use rng to get a index between 0 to current array.size()
// 2. remove the string by array.remove(index) so that it won't duplicated.
// 3. setText(string)
}
Try this, you can add comments if you need more details.
One way to do it is to convert the array to a list and shuffling it.
List myList = Arrays.asList(yourArray);
Collections.shuffle(myList);
After that you can just iterate over the list.

Android random but repeat 2 times

I have a little question concerning the usage of
Android Resouce by ID / Change image onClick / no change of imageView
I have established my images picked randomly here, using:
#Override
public void onClick(View v) {
Log.d("MYAPP", "Like-Button clicked");
/*imageViewMeasurement.setImageResource(R.drawable.p13);*/
TypedArray images = getResources().obtainTypedArray(R.array.images_primes);
int chosenImageNumber = (int) (Math.random() * images.length());
// setImageResource to the random chosenImageNumber
imageViewMeasurement.setImageResource(images.getResourceId(chosenImageNumber, R.color.colorPrimaryDark));
images.recycle();
// Confirmation if the random generator picked a Number from the array
String chosenImageNumberTest = String.valueOf(chosenImageNumber);
Log.d("MYAPP Choice Number", chosenImageNumberTest);
}
This runs through an array of 40 images and will be repeated one time. So every image will be shown two times (?).
That's the question:
When I use a pool of 40 images randomly for 80 picks, do I get every image two times (draw with cover), or is every try a new random out of those 40 images (draw without replacement), so the reult could be number 1 for 4 times and number 38 for 0 times? Is there an other function that prevents to such a behaviour?
Best,
tigercode
As I understand your code, you would NOT get every image twice, you would get some images multiple times and some images might not even come at all.
Don't use Random if you don't actually want Random. By the laws of probability you'd only have a chance of getting the same image twice, not a certainty.
You could use a boolean array to keep track of which numbers have already been used (if index n is true this means the number has already been taken e.g.).
Edit: The comment below from vims liu is right. It's much more efficient run time wise to define a list of indexes and shuffle the list.
So better use the following solutions, even if it won't make a big differences considering your numbers are quite small.
List<Integer> indexes = Arrays.asList(1,2,3,4,5,6,7,8,9,10); //...
Collections.shuffle(indexes);
You can then iterate through the indexes list and use the current number as current index.
Sorry for answering late, and sorry for a beginner question, I'm new to android developing.
I changed my code (and write my comment what I understood it does:
#Override
public void onClick(View v)
Log.d("MYAPP", "Like-Button clicked");
// 1. Get the array of images in the images_primes XML-List AS int (number of every image in array)
TypedArray images = getResources().obtainTypedArray(R.array.images_primes);
// 2. Takes an array-list and shuffles it
List<Integer> indexes = Arrays.asList(1,2,3,4,5,6,7,8,9,10); //...
Collections.shuffle(indexes);
// Great, it shuffles! ;-)
Log.d("MYAPP", indexes);
// 3. takes the number from the shuffled image list
int chosenImageNumber = (int) (indexes);
// 2. Old code: picks a number out of the image array from 1. / commented out
//int chosenImageNumber = (int) (Math.random() * images.length());
// 4. setImageResource to the random chosenImageNumber
imageViewMeasurement.setImageResource(images.getResourceId(chosenImageNumber, R.color.colorPrimaryDark));
images.recycle();
// Confirmation if the random generator picked a Number from the array
String chosenImageNumberTest = String.valueOf(chosenImageNumber);
Log.d("MYAPP Choice Number", chosenImageNumberTest);
}
Step 1: Open the list of images (as items), use it as array (1,2,3)
Step 2: Have a second number of arrays (possible to connect 1 and 2), SHUFFLE! :-)
Step 3: Here is my problem. If I got that right, I get a string-list from step 2, which numbers I can't use as Int (Error "Integer to int" won't work) -> result should be a number (?)
Step 4: Number from 3 should be used to pick out an image from list / array from 1.
I think, there is a thinking error on my side.
Thanks for your help in advance,
tigercode

Random images with no repetition (image guessing)

Hello guys I'm new to android development and java programming. I hope someone can help me. I'm developing an app which is an image guessing game. I have 100 images stored in my drawable.
I currently have this code that will randomly generate an image and it will be displayed in the images view:
ImageView random_image = (ImageView) findViewById (R.id.random_level);
final int[] images = { R.drawable.img1, R.drawable.img2...R.drawable.img100 };
Random generator = new Random();
random_image.setImageResource(images[generator.nextInt(images.length - 1)]);
My question now is how can I avoid duplication of image in every level? My game composes of 25 levels.
Maybe create an ArrayList of those Image id's and whenever a user guesses an image, just remove that Image id position from that ArrayList
Example:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.your_images); //keep adding that, maybe using a loop would be better to add
int position = new Random().nextInt(list.size());
imageViewObject.setImageResource(Integer.intValue(list.get(position)));
//while starting next level
list.remove(position);
That's all!
If you use Random class there is a chance for repetition.
So store them into a ArrayList .So you can use Collections.shuffle(list);
It will shuffle the items in the ArrayList. So ,no head ache regarding repetition.

Display 20 random images over UI screen at a time in Android

HI,
I want to display 20 random images at a time over Activity screen in android. I'm using this snippet:-
ImageView imageArr[] = new ImageView[25];
int id[]={ R.drawable.letter1,R.drawable.letter2,R.drawable.letter3,R.drawable.letter4,
R.drawable.letter5,R.drawable.letter6,R.drawable.letter7,R.drawable.letter8,
R.drawable.letter9,R.drawable.letter10,R.drawable.letter11,R.drawable.letter12,
R.drawable.letter13,R.drawable.letter14,R.drawable.letter15,R.drawable.letter16,
R.drawable.letter17,R.drawable.letter18,R.drawable.letter19,R.drawable.letter20};
ArrayList<Integer> randomArr = new ArrayList<Integer>();
for(int i=0; i<20;i++) {
randomArr.add(i);
}
Collections.shuffle(randomArr);
for(int i=0; i<20; i++){
//ImageView mImageplay = (ImageView)findViewById(R.id.image_play);
imageArr[i]=(ImageView)findViewById(id[randomArr.get(i)]);
//mImageplay.setImageResource(id[randomArr.get(i)]);
}
Here I'm having 20 custom images of size 49x49 pixels(small sizes)in drawable folder which i want to dispaly over UI at a time in random fashion with no alignment.
The problem is in imageArr[i] is showing NULL value for all the 20 images,whereas "randomArr" is having the correct shuffled data.
Also, Is there any way to display it in randomized look in Layout Area as i was not able to find out solution for this.
I am struck in this and not able to resolve it. please help me out for this.
thanks in advance.
-pk
Your array id[] contains R.drawable. values (id of Drawables in your app) instead of R.id. (id of your ImageViews in your layout).
Set the android:id attributes in your layout, and use them in your id[] array.

How to show random images on Android?

I have a number of images in my directory.
I want to show random images in ANDROID.
Please anyone provide me with an example.
Assume that your images are named img1.png, img2.png, and so on, and they are located in res/drawable folder.
Then you can use the following code to randomly set an image in an ImageView
ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(n) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
I don't have an example but I can provide you an idea.
Build a list of images in an array
Generate a random number between 0 to 1 less than the number of images in folder
Use the random number in step 2 as an index to the array and pick up the image for display.
You have to combine some things. First you need an ImageView in order to display an image on the Android phone.
Then I would take a look into a random number generator (e.g. http://docs.oracle.com/javase/6/docs/api/java/util/Random.html) so that you can get a random number.
By combining these to things, you can randomly select a picture from a list of available pictures and display it using the ImageView.

Categories

Resources