Display random word from list - android

I am a beginner and I am making a an android app. My goal is to have the app randomly return a specific word from a list of word when a button is pressed. How should I go about doing this? Also, I am using eclipse and do not have much experience at all.

Since you already mentioned in your question that you want to generate a word from a list of words, I assume that you already have the list prepared.
Use the following to generate a random number and then display the word corresponding to that index.
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100); // maximum value 100; you can give this as the maximum index in your list of words
Note : do not use Math.random (it produces doubles, not integers)
import java.util.Random;
EDIT :
Assume that you have the list of words in a String array which contains 30 words.
String wordList[] = new String[30];
// enter the words in the wordList (if you have a String, use a Tokenizer to get the individual words and store them in wordList)
int randomInt = randomGenerator.nextInt(30);
String wordToDisplay = wordList[randomInt];
Then, you can display wordToDisplay in your TextView by using mTextView.setText(wordToDisplay);

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.

Gerator Image Random

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.

Create List of Remaining Values from List after generating Random numbers in android

I am creating "Jacks Or Better" game in android, in that i am generating 5 random numbers from list of 52 numbers and storing it to a list now i want to store remaining 47 numbers in other list so that i can again generate random numbers from the list of remaining numbers. i did following code,
Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
while (true) {
Integer next = rng.nextInt(52);
if (!generated.contains(next)) {
generated.add(next);
openCards.add(CardArray.get(next));
break;
}
}
}
The above code is for generating 5 random numbers and storing it into a list but i don't have any how to store remaining 47 numbers into other list.
please help me.
You probably want SecureRandom instead.
Reimplement CardArray as a CardSet and remove from it as you "pull" the cards. You can add cards back to the set or you can simple clone the CardSet before use depending on how you want to optimize space vs compute.

how to create dictionary words with random letters in android?

I am creating a small word game for my self.I am generating random letters.
Here code for generating random letters:
//GENERATE RANDOME LETTERS
mRnd = new Random();
mLetters = 25;
String randomLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for ( n=0; n<mLetters; n++)
System.out.println("Random letters"+randomLetters.charAt(mRnd.nextInt(randomLetters.length())));
Here i want how to generate some meaning full words with this random letters Like(CAT,BAT,RAT).
You should download a dictionary and load it at runtime. There is no code to validate if the word exists, you can only compare it to a known list.
Here you can download a English dictionary: http://www.curlewcommunications.co.uk/wordlist.html

Displaying random questions in an application

I am developing an application which involves a quiz in it. I have used a viewflipper for displaying question, as i press the next button it flips to next question. However, I wish to display these questions randomly. I have parsed an xml and displayed the questions n its options !
Assuming that all your questions stored in array or some data structure e.g. ArrayList
You need to define a single random object only once in your activity
e.g.
Random random = new Random();
Get the next question index:
int nextQuestionIndex = random.nextInt(n); // where n is the total number of questions

Categories

Resources