0

I working on a project in c# asp.net. I would like to randomly display a string from either an array or list, if certain conditions are met, I would like to display another random string that has not been displayed.

EX.

 LIST<string> myString = new List<string> {"one", "two", "three", "four", "five"};

or

string[] myString = new[] {"one", "two", "three", "four", "five"};

void btnAnswer_Click(Object sender, EventArgs e)
{
string Next = myString[random.Next(myString.Length)];
if(my condition is met)
{
lbl.Text = Next;
}
}

I can randomly call a string from my list or array, but not sure how to store repeat results. I've tried using an algorithm that jumbles the array and then a counter for the index, but the counter (counter++) seems to add 1 once and then stops. I've tried using a list and removing the string I use, but it seems to repeat after all strings have been used.

If more code is needed I can provide, just need a point in the right direction.

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54

4 Answers4

0

Couple of options:

  1. Have an array or list which is a copy of the master array, and whenever you choose one, remove it from the array. Once the array is empty, refresh it from the master array.

  2. Similar, just create your array, shuffle it, and start giving out strings by order. so array[0], next would be array[1], and so on, once you reach the end, shuffle and start again.

There are probably others as well :)


Edit:
If I understand correctly from the comments, you are talking about the option where the input is not unique to begin with. If that is the case, you can create your list with a simple linq query to get only unique values (using distinct), guaranteeing no duplicates.

Have a look at this question for an example.

Community
  • 1
  • 1
Noctis
  • 11,507
  • 3
  • 43
  • 82
  • But do any of these guarantee that same won't be repeated again ? – tariq Dec 02 '13 at 06:43
  • Lets see: **option 1)** you remove what you show. You wont see them again (unless you run out of options and refill the list). **Option 2)** you shuffle and just return the next index. Same thing. Not unless you finish all and start again. :) – Noctis Dec 02 '13 at 06:45
0

You can copy the indices into a list, it will save memory while allow us to track all the remaining items:

var indices = Enumerable.Range(0,myString.Count).ToList();
//define some method to get next index
public int? NextIndex(){
  if(indices.Count == 0) return null;
  int i = random.Next(indices.Count);
  int k = indices[i];
  indices.RemoveAt(i);
  return k;
}
if(my condition is met) {
 int? nextIndex = NextIndex();
 lbl.Text = nextIndex == null ? "" : myString[nextIndex.Value];
}

Note that the Text is set to empty if there won't be no more remaining string, however you can handle that case yourself in another way such as keep the text unchanged.

King King
  • 61,710
  • 16
  • 105
  • 130
0

One option is to display one of the random string and store it in a ViewState.

When you come next time again, check whether new random string is already there in ViewState or not. If it is there in ViewState then get another random string.

SpiderCode
  • 10,062
  • 2
  • 22
  • 42
0

You could probably do that, but why not get a unique list of strings first?

var uniqueStrings = myString.Distinct().ToList();

Then as you select strings, do a .Remove() on the last randomly selected value from uniqueStrings.

You said that:

I've tried using a list and removing the string I use, but it seems to repeat after all strings have been used.

The problem here is using the same instance of random for your series after you've run out. If you re-instantiate random = new Random(), the variable is re-seeded and you will have totally different results from what was generated before.

Jon Limjap
  • 94,284
  • 15
  • 101
  • 152
  • i don't think that having unique strings is the primary requirement here, I feel OP is trying to create may be some chat bot type stuff and wants to ensure the it doesn't give the same response again one after the other.. – tariq Dec 02 '13 at 06:54
  • @tariq Touche, that is weird, but " I've tried using a list and removing the string I use, but it seems to repeat after all strings have been used." looks more like a random seeding problem more than anything... – Jon Limjap Dec 02 '13 at 07:22
  • everyone is so interested in this question except the OP i guess :D – tariq Dec 02 '13 at 07:27