0

I want to randomly be able to return a person's name in a map using the method randomizeName(int group); I want it to randomize the Strings in a Map called people.
Here is how people looks:

public Map<Integer, String> people = new HashMap<Integer, String>();

There are three groups in people, 1, 2, and 3.
There are nine Strings in this HashMap, each associated with a group:
Greg, George, Fred, Marie, Ann, Julie, Alfred, Bob, and Julian.
How do I return a randomized String only in group one, for example.

So instead of it randomizing the Strings in the entire Map, how do I make it only randomize the people in group one(Greg, George and Fred)?

Here is the method:

public String randomizeName(int group){
//Enter Code Here
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
jl00
  • 105
  • 1
  • 1
    You need t make an effort to solve the problem yourself before posting. Also, your question is very unclear. What output do you want to see? – Jim Garrison Apr 07 '14 at 18:13
  • How are you keeping track of what group people are in? You understand that `people.put(1, "Greg"); people.put(1, "George");` won't store both entries? – azurefrog Apr 07 '14 at 18:14
  • Yes, and also can you please post what the key for your hash map is? What does that integer correspond to? – Scott Shipp Apr 07 '14 at 18:15
  • Maybe a different design would be better: A class which is a Group and contains a List of all members, and outside another List with all groups. What about it? – Marco Acierno Apr 07 '14 at 18:16
  • Names in a Map are not random. What do you mean by "random" Strings? – Elliott Frisch Apr 07 '14 at 18:17
  • You need either a Map> or a multimap from e.g., Guava. Try building each piece individually, such as creating the map, finding out how many names are in a group, calculating a random number in the desired range, and getting the name from a group at a given index. Those are the pieces you need. – David Conrad Apr 07 '14 at 18:48

2 Answers2

0

I will help you with following details...

First I think you need to create a proper hash map, your map can contain only single person per group id

Task for you : change the map such that per key(group id 1, 2 or 3) it can contain more number of member names

second you can use random number generation within a range and use it to decide what name you want to send refer How can I generate random number in specific range

Task for you : code such a way that for different random number you can send different user name

Community
  • 1
  • 1
dev2d
  • 4,245
  • 3
  • 31
  • 54
0

Mean you want

public String randomizeName(int group){
//Enter Code Here
} 

should return any String of group one that is (Greg, George and Fred). Right? If yes then you can use Math library and use random function which return a double variable. 0<=value>=1 ie value would be either 0 or greater and 1 or lesser. So you can make condition. if math.random is 0 then Greg and if math.random 1 then George and if (0math.random) then Fred. You can refer this link as well http://www.tutorialspoint.com/java/lang/math_random.htm