There are 5 people and 5 things. 5 people are
Mg Mg, Su Su, Zaw Zaw, Kyaw Kyaw, Mya Mya
5 things are
Iphone, mp3, tv and two thank you
I want each person to get a thing randomly like lucky draw every time I run this in Java. I can't get the same people to get the same thing if I run the second time. I am not sure if I can use the array method for names or things to store them.
Can you tell me how to do that? Can I get the code if possible?
I've already tried this but have no idea how to continue.
ArrayList<String> name = new ArrayList<String>();
name.add("Mg Mg");
name.add("Su Su");
name.add("Zaw Zaw");
name.add("Kyaw Kyaw");
name.add("Mya Mya");
ArrayList<String> name = new ArrayList<String>();
name.add("Mg Mg");
name.add("Su Su");
name.add("Zaw Zaw");
name.add("Kyaw Kyaw");
name.add("Mya Mya");
ArrayList<String> items = new ArrayList<String>();
items.add("Iphone");
items.add("mp3");
items.add("tv ");
items.add("Iphone4");
items.add("Iphone5");
int rand = (int) (Math.random() * 5);
Every time you have to generate Random number, you have to save rand number into DB/file. And check that new generated no is in file or not. If it is not there then use that number for mapping and it is there then generate again the number.
Related
I'm working on a tool about humanitarian logistics. In this model I have some lorries which pick items for support affected people by an earthquake and, after picking them, go to eartquake epicenter to drop these items. I need to manage the availabilty of these warehouses: for example, if a warehouse has 5 items availables and lorries has a transport capacity by 2, availabilty have to become 3 for that warehouse. I need to realize obviously this process for all warehouse of my Supply Chain. I've dropped (as you can see in the pic that I've uploaded) a parameter (availability) in the class of the warehouses [named Magazzini]).
This is the algorithm that manages lorries movement, in which I need to code this command to change availabilty.
List <Magazzini> subsetlist = findAll(main.magazzinis, w->w.capacita>0);
List <Magazzini> sortmag = new ArrayList<Magazzini>();
List <Double> distance = new ArrayList<Double>();
sortmag = subsetlist;
System.out.println(sortmag);
for (Magazzini m : subsetlist)
{
m.distance = distanceTo(m);
}
sortmag = sortAscending(sortmag, p-> p.distance);
//main.magazzinis.cap = main.magazzinis.cap - 2;
moveTo(sortmag.get(0));
System.out.println(sortmag);
partenza = time();
I write a possible command to do it, but it doesn't work. How can I fix it?
I'm writing this golf program for my class that takes a .txt that has 5 numbers per row and 18 rows. The first number in each row is the par for that hole. The other four numbers are for each player.
I kind of have my own spin on this program, though. I wrote another program to create the .txt file with random numbers. The output is dependent on how many players there are which is inputted by the user.
Anyway, I've gotten the .txt generator just fine and I've gotten the Golf program to accurately count how many players there are. What I can't figure out is how to create that number of arrays with different names. I want each array to be int player1[18], player2[18], player3[18], etc.
Here's my code up to that point:
import java.util.Scanner;
import java.io.*;
public class Golf
{
public static void main(String[] args) throws java.io.IOException
{
Scanner countScan = new Scanner(new File("golfscores.txt"));
Scanner file = new Scanner(new File("golfscores.txt"));
//------------------------------------------------------------
// Counting the number of players
//
// This takes the number of integers in the file, divides it by
// the 18 holes in the course and subtracts 1 for the par.
//
// I needed to count the players because it's a variable that
// can change depending on how many players are entered in the
// java program that creates a random scorecard.
//------------------------------------------------------------
int players = 0;
for (int temp = 0; countScan.hasNextInt(); players++)
temp = countScan.nextInt();
players = players/18-1;
//------------------------------------------------------------
//Creating necessary arrays
//------------------------------------------------------------
}
}
EDIT: I must use an array for each player and I am not allowed to use ArrayLists. At this point it looks like I will be using an array of arrays as suggested by some in the comments. Didn't know this was a thing (obviously I'm very noob).
Well you can use a HashMap to store your arrays. Or if you don't care about using strings to get to the array just use 2D Arrays like this:
int[][] players = new int[playerCount][18];
If you then use for example player 2 and want to see hole 12, you'd call players[1][11]
You should not go that way.
Instead create a class named Player to hold each player properties, then create a list of players: List<Player> players = new ArrayList<>();
Add each new player to that list.
I have a simple educational kids game with 7 questions. One imageView and four buttons for each question, the user must match the correct answer(button) with what is shown in the image view. I want the images(questions) to be random every game but never repeat a question during a game until all 7 have been asked. I am only using 3 images as of now just to get it working.
Option 1
int[] res = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
Method
private void randomImage() {
Random rand = new Random();
int rndInt = rand.nextInt(res .length);
imgView.setImageDrawable(getResources().getDrawable(res[rndInt]));
}
Option 2
private ArrayList<Integer> res1 = new ArrayList<Integer>();
res1.add(R.drawable.img1);
res1.add(R.drawable.img2);
res1.add(R.drawable.img3);
Method
private void randomImage1() {
Collections.shuffle(res1);
for(int i=0;i<res1.size();i++){
imgView.setImageResource(res1.get(i));
}
}
Both of these work for randomizing, but I am having a little trouble figuring out how to check if an image has already appeared and to correct it if it had.
In fact I'm not really quite sure where to start. Any help would be appreciated.
If you dont want to see repeated items from array then use shuffleArray() like this example and for a list use shuffle(list) like this example2
I am creating a old school game where the user has to collect the falling objects. Currently I have an image that is printed to the GraphicsContext several times accross the pane and is added and removed from an arrayList() when it disappears off of the screen. I can create a random number and I can print this to the same position as the falling image. However the random number is always the same and I want the number to be different on each of the objects. My code in my start method is as follows:
int i;
for(i=0;i<800; i+=90)
arrayList.add(new Object(ImageView, noOnImage, i,-10));
I then also have an update method where the objects are continously redrawn:
int newObject = 0;
Iterator<Object> objectIterator = object.iterator();
while(objectIterator.hasNext())
{
Object ob = objectIterator.next();
if(ob.move())
{
objectIterator.remove();
newObject++;
}
gc.drawImage(ob.objectImage,ob.r.getX(), ob.r.getY(), ob.r.getWidth(),
ob.r.getHeight());
gc.fillText(String.valueOf(noOnImage), ob.r.getX()+8, ob.r.getY()+22);
}
noOneImage is just a randomly generated number that I declared at the top. I would like each object to contain a different random value, currently they are all the same though, even though it is random.
When you create the object, pass in the random number within your loop.
Random rand = new Random()
arrayList.add(new Object(ImageView, noOnImage, i,-10, rand.nextInt(0, 10)));
That rand will give you a number between 0 and 10. You can put whatever int you want in there.
And make sure the object you create has the field and you are able to retrieve this number (e.g. for a score tally, etc.).
So my professor gave out an assignment in which we have to create an operating slot machine program. The machine has 3 reels, which themselves consist of 6 six symbols stored in an enum:
public enum Symbol
{
SEVEN(12,"images/seven.jpg"),
WATERMELON(10,"images/watermelon.jpg"),
ORANGE(8,"images/orange.jpg"),
PLUM(6,"images/plum.jpg"),
LEMON(4,"images/lemon.jpg"),
CHERRY(2,"images/cherry.jpg");
}
We're supposed to use these symbols to create a "reel" object. My problem is centered around populating this reel (which is really just a Symbol[] array) with the symbols in the order requested by the professor.
He asked us to use the Random class to populate the reel using 10 as the seed number. Here's what I have so far:
//Creating an unrandomized reel array, which contains the symbols in the exact order as they appear in the enum
private Symbol[] unrandomizedReel = new Symbol[]{Symbol.SEVEN, Symbol.WATERMELON, Symbol.ORANGE, Symbol.PLUM, Symbol.LEMON, Symbol.CHERRY};
//Declares local Symbol array
Symbol[] randomizedReel = new Symbol[6];
//Keeps track of the position in the unradomized reel array
int unrandomizedReelIndex = 0;
//Creates a Random object which will be used to generate values based
//on the seed. **seed == 10**
Random randNum = new Random(seed);
//Loop will populate the randomized reel
while(unrandomizedReelIndex < 6)
{
//This int will hold values ranging from 0-5. Will be used as an index
//to populate randomized reel
int rangedRandomNumIndex = randNum.nextInt(6);
//if the element at the given index in the randomized reel is empty
if(randomizedReel[rangedRandomNumIndex] == null)
{
//A symbol from the unrandomized reel is added to it
randomizedReel[rangedRandomNumIndex] = unrandomizedReel[unrandomizedReelIndex];
//The index used to iterate through the unrandomized reel is incremented by 1
unrandomizedReelIndex++;
}
}
Running this "shuffling" code, I get the following output:
WATERMELON
PLUM
CHERRY
SEVEN
ORANGE
LEMON
However, according to my professor, the output should be:
ORANGE
PLUM
SEVEN
LEMON
WATERMELON
CHERRY
What am I doing wrong? Why is my output different from his, even though we are both using 10 as a seed? Any help would be appreciated. Thanks.
new Symbol[]{Symbol.SEVEN, Symbol.WATERMELON, Symbol.ORANGE, Symbol.PLUM, Symbol.LEMON, Symbol.CHERRY};
can be simplified to
Symbol.values()
My guess would be that your professor is putting
unrandomizedReel[Random.nextInt(6)]
in position 0, then nulling that out that element of unrandomizedReel, getting a random number x between 1 and 5 and taking the x-th non-null value and putting it in position 1, nulling it, then repeating with a number between 1 and 4 and so on.
If you have a reason to believe your professor is using a different algo, then edit your question with that information.