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.
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);
I encountered a problem in android, for example:
Number 78900000 will show 7.8E7 and not the number itself.
DecimalFormat df2 = new DecimalFormat("#########.00");
dd2dec = new Double(df2.format(number).doubleValue());
How can I format the number and show it in original form without in scientific form ?
I believe this might help:
format("%,d", (number)); //Displays with commas placed appropriately.
format("%,d", (number)); //Displays un-formatted integer.
Android Formatting Documentation
I've managed to get my allocations down to next to nothing using DDMS (great tool), this has drastically reduced my GCs to about 1 or 2 every 3 minutes. Still, I'm not happy because those usually cause a noticeable delay in the game (on some phones) when you interact with it.
Using DDMS, I know what the allocations are, they are Strings being converted from integers used to display game information to the HUD.
I'm basically doing this:
int playerScore = 20929;
String playerScoreText = Integer.toString(playerScore);
canvas.drawText(playerScoreText, xPos, yPos);
This happens once each frame update and the HUD system is modular so I plug things in when I need and this can cause 4 or 5 hud elements to allocate Strings and AbstractStringBuilders in DDMS.
Any way to reduce these further or eliminate all the String allocations and just reuse a String object?
Thanks,
Albert Pucciani
Reading your question reminded me of one of Robert Greens articles that I read quite some time ago. It discusses your problem almost identically. http://www.rbgrn.net/content/290-light-racer-20-days-32-33-getting-great-game-performance . Skip down to day 33 and start reading.
Remember the last int score and its string representation. On a new frame check if the score is the same. If the same, then no need to create a new string - just use the old one.
Here's what I've done in the past. This will eliminate string allocations.
I create a char[] of a size that will be at least as large as the maximum number of characters you will need to display on the screen. This means that you should select a maximum high score that is achievable in the game. The way you have it now let's you display a score as high as 2^31-1 which is insanely huge, it's not practical with respect to the game. Keep in mind, this is your game, so it's ok to limit the max score to something more reasonable in the context of the game. Pick a number that will virtually be impossible to achieve. Setting this limit will then set you up to be able to not have to muck around with converting large integers to String objects.
Here's what's required:
First, you need to be able to separate the digits in an integer and convert them to char without creating String objects. Let's say you want to convert the integer of 324 into three separate characters '3','2','4' to be placed in the text char[]. One way you can do this is by taking the value 324 and do a mod 10 to get the lowest digit. So 324%10 = 4. Then divide the value by ten and do another mod 10 to get the next digit. So (324/10)%10 = 2, and (324/100)%10 = 3.
int score = 324;
int firstPlaceInt = score%10; // firstPlace will equal 4
int tensPlaceInt = (score/10)%10; // tensPlace will equal 2
int hundresPlaceInt = (score/100)%10; // hundredsPlace will equal 3
You will have to do the above in a loop, but this expresses the idea of what you're trying to do here.
Next, with these digits you can then convert them to chars by referencing a character map. One way to do this is you can create this character map by making a char[] of size 10 and placing values 0 - 9 in indexes 0 - 9.
char[] charMap = {'0','1','2','3','4','5','6','7','8','9',};
So doing this:
int score = 324;
char firstPlace = charMap[score%10];
char tenslace = charMap[(score/10)%10];
char hundredsPlace = charMap[(score/100)%10];
Will create the chars you need for the 3 digits in score.
Now, after all that, I would limit the highest score to say 99,999 (or whatever makes sense in your game). This means the largest "string" I would need to display is "Score: xx,xxx". This would require a char[] (call it text for this example) of size 13. Initialize the first 7 characters with "Score: ", these will never need to change.
char[] text = new char[13];
text[0] = 'S';
text[1] = 'c';
text[2] = 'o';
text[3] = 'r';
text[4] = 'e';
text[5] = ':';
text[6] = ' ';
The next 6 will vary based on the score. Note, that you may not necessarily fill in all 6 of those remaining characters, therefore you need to create an int (call it scoreCount for this example) which will tell you how many characters in the text char[] are actually relevant to the current score in the game. Let's say I need to display "Score: 324", this only takes 10 chars out of the 13. Write the 3 chars for the score of 324 into char[7] to char[9], and set scoreCount to 10 to indicate the number of valid characters in the char[].
int scoreCount = 7;
text[9] = charMap[score%10]; // This is firstPlace
text[8] = charMap[(score/10)%10]; // This is tensPlace
text[7] = charMap[(score/100)%10]; // This is hundredsPlace
scoreCount = 10;
You will probably have to do the above in a loop, but this should express the general idea of what you're trying to do here.
After that, you can just use drawText (char[] text, int index, int count, float x, float y, Paint paint). index will be 0, and count will be scoreCount which indicates how many characters in text should be drawn. In the example above, it doens't matter what's in text[10] to text[12], it's considered invalid. You can continue to update text[] using the character map, and this should not create any objects.
I hope this helps. The code above isn't very robust, but I wrote it out as more of an expression of the ideas I'm trying to convey. You will have to create your own loops and manage the data properly within your code, but this sums up the mechanics of what needs to happen to avoid the use of Strings/StringBuilder/StringBuffer/etc.
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.