1

I'm sort of new to programming and I may be trying to run before i can walk but basically I have a list of strings and at the end of each printed line I'd like to randomly select one of the strings. I did this and it did pick a random string but the problem is every time it's used in the program it sticks to that one random string.

    String items[] = {"bla bla", "asdnoaisd", "anafgsfsan"};
            int amount;
            String list;
            Random r = new Random();

    amount = (int) (Math.random()*25+1);

    list = items[r.nextInt(12)];

help would be appreciated

UPDATE - so I probably worded this wrong. the problem isn't getting a random string but I need it to reset after every time it's used, so after it's used once it should reset and pick another string from the list.

dimo414
  • 47,227
  • 18
  • 148
  • 244
waterytart
  • 21
  • 6
  • 2
    check this one: http://stackoverflow.com/questions/6726963/random-string-from-string-array-list – solar Aug 17 '15 at 20:43
  • We need more information to help you with this – ControlAltDel Aug 17 '15 at 20:45
  • If you've resolved your problem, please either select an answer or post your own explaining what you found out. If you just say it's "resolved" no one else can benefit from your post in the future. – dimo414 Aug 17 '15 at 22:00

6 Answers6

1
Random r = new Random();

creates a pseudo-random list of values. To get other results with each run of the program use:

Random r = new Random(System.currentTimeMillis());

instead.

Niklas P
  • 3,427
  • 2
  • 15
  • 19
  • There's no need to seed `Random` objects in modern JVMs (Java 7 and 8, at least). And `Random` provides a source of random data, not a list of values. – dimo414 Aug 17 '15 at 20:52
  • I need it to get other results in the same run. – waterytart Aug 17 '15 at 20:58
  • @dimo414. `Random` does not provide a `List` of values. But it does indeed provide a list of values. – Anonymous Coward Aug 17 '15 at 21:04
  • "[List](https://en.wikipedia.org/wiki/List_%28abstract_data_type%29)" in any CS context comes with a number of attributes which [`Random`](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html) does not provide (for instance it can be backed by actual sources of randomness). Further, it explicitly provides bits, not "values" which isn't even meaningful in this context. It's totally ok to use layman's English to get your general point across, but neither "list" nor "values" is correct. I'm just offering a clarification, there's no need to defend your use of common English. – dimo414 Aug 17 '15 at 21:58
1

You're asking to select a random value from a list, and you mention wanting to "reset" after each selection. This is commonly known as "sampling with replacement" meaning that when you select a sample you don't change the probability that you'll select it again the next time. Imagine a drawing cards from a deck - with replacement you put the card back and reshuffle, without replacement you set it aside and draw from the (now smaller) deck.

With-replacement sampling is easy to implement in Java, simply use Random.nextInt(int) to select a random value between 0 and the size of your list. The returned integer is the index to get from your list.

Without-replacement sampling is a little tricker (or requires a different data structure) since you have to either modify the original list or store the set of previously-seen values. which you should prefer depends on how many values you'll need to select.

Several people have suggested Collections.shuffle() which also works, but is more invasive than simply generating a random index. Random.nextInt() is O(1) and doesn't modify your list in any way. Collections.shuffle() has to iterate over the entire list, taking O(n log n) time, and modifies your list. It's useful if you need to select many values from the list, or want without-replacement selection (in which case you shuffle once, then simply iterate over the list).

dimo414
  • 47,227
  • 18
  • 148
  • 244
0

The simplest way to do that would be to use time,

list = items[(int)(System.currentTimeMillis() % items.length)];
Tawcharowsky
  • 615
  • 4
  • 18
0

Are you initializing the random string with list = items[r.nextInt(12)] ? When do you actually use list ? I wouldn't even use list I would just plug in the line items[r.nextInt(12)]; When you need to acess it and use it that way it will generate a new random number each time rather than I initializing it with one random number and using that for the rest of your program.

Mike James Johnson
  • 724
  • 2
  • 8
  • 29
  • here's the rest of the code. System.out.println("Enter, first number" + (list)); and that repeats a few times but sticks with the same String – waterytart Aug 17 '15 at 21:08
  • System.out.println("Enter, first number" + items[r.nextInt(12)]); – Mike James Johnson Aug 17 '15 at 21:14
  • Good! Your problem was that you set your string, list, equal to another string and then you printed it so of course it'll be the same each time. So really you only accessed your random number object once when it initialized the string. Hope that clears it up. Good luck – Mike James Johnson Aug 17 '15 at 21:26
0

Instead of an array, use a List.

Then, use Collections.shuffle(). Now simply iterate through your list.

Akshay
  • 3,158
  • 24
  • 28
0

If you addMockNeat as a dependency you can use the from() method. It might be overkill to do that if it's only a one-time thing to do. Then I would prefer to write my own one-liner as other have suggested in other answers.

String[] arr = {"abc", "acd", "adf" };
String s = m.from(arr).val();
// Possible Output: "adf"

Disclaimer: I am the author of the library, so I might be biased when I am recommending it.

Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118