random limited repetition from array...

  • Thread starter Thread starter mj.clift
  • Start date Start date
M

mj.clift

Hi All,

I need to be able to choose a random string from an array. That is easy
enough, but I want to restrict the repetition of that string until one
or two other choices have been made. So the following output would be
ok a,b,c,a,d,c or a,c,d,a,d,a but the following would not; a,b,c,c, or
a,b,b,c, etc... I hope that makes sense.

If it helps to understand what I'm trying to do I have the following
python code.

for x in NumberChoices :
while FirstChoice in state[0:2]: #or for x in range(2):
FirstChoice = choice(NextChoice[Choice_List[-1]])
Sequence_List.append(FirstChoice)
state[x%2] = FirstChoice

I've just started with c# so any help would be of great help.

Thank you,

Calvin
 
Hi All,

I need to be able to choose a random string from an array. That is easy
enough, but I want to restrict the repetition of that string until one
or two other choices have been made. So the following output would be
ok a,b,c,a,d,c or a,c,d,a,d,a but the following would not; a,b,c,c, or
a,b,b,c, etc... I hope that makes sense.

Almost, but there is some information missing. From what you wrote I
understand that you want to:

Have as input a set of characters
Prepare a string for output, initially empty
Repeatedly choose a random element from this set, and append it to a
string for output
- with the limitation that an element should not be immediately
repeated
At some point - stop, and return the string

The outstanding question is: when do we stop? Taking your example of
abcadc as valid output - would abcad be valid? abca? abc? Tell us the
rule for when to stop and we can proceed.
If it helps to understand what I'm trying to do I have the following
python code.

Python is still on my -to do- list, unfortunately :)
 
Hi Larry,

Thanks for your offer of help.

Larry said:
The outstanding question is: when do we stop?

We would keep choosing based on an input int "number of choices" from
an array of say 10 strings. In my python code it is "for x in
NumberChoices".

Thanks,

Calvin
 
Hi Larry,

Thanks for your offer of help.



We would keep choosing based on an input int "number of choices" from
an array of say 10 strings. In my python code it is "for x in
NumberChoices".

OK. So the number of elements in the output is just another parameter.
I note you keep saying 'strings' though your example used letters,
which would be Chars; I will use Strings for generality.

using System;
using System.Collections.Generic;
using System.Text;

namespace AlmostRandomTest
{
class Program
{
static string AlmostRandom(int desiredLength, string[]
elements)
{
// We need a Random to make random choices
Random rng = new Random();

// Some might insist on a StringBuilder since we are
// going to be concatenating strings, but this is just
// example code
string output = ""; // or String.Empty - religious war

// Each element chosen must not be the same as the
preceding
// element
int lastIndex = -1;
int thisIndex;

// Produce desiredLength elements:
for (int i = 1; i <= desiredLength; i++)
{
// choose a random element
// that isn't lastIndex
do
{
thisIndex = rng.Next(elements.Length);
}
while (thisIndex == lastIndex);

// now thisIndex is an element different from the
// preceding element
output += elements[thisIndex];

// remember the element just used
lastIndex = thisIndex;
}

// our output string is complete
return (output);
}

static void Main(string[] args)
{
// Test harness for AlmostRandom
string[] elements = { "a", "b", "c", "d" };
string output;
bool allOK = true;

for(int i = 1; i <= 100; i++)
{
// test includes asking for zero length output
output = AlmostRandom(i % 10, elements);

// wait a bit so random seed will be different
// next time round
System.Threading.Thread.Sleep(10);

Console.WriteLine(output);

// check for repeated elements
if(output.Contains("aa")
| output.Contains("bb")
| output.Contains("cc")
| output.Contains("dd"))
{
// oops
Console.WriteLine("******** failure");
allOK = false;
}
}

if(allOK)
Console.WriteLine("All OK");

// wait for <enter> to exit
Console.ReadLine();
}


}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top