-
November 13th, 2008, 11:39 PM
#1
Any idea how to randomize this?
My Goal is for this data to be randomized every time it is read into the array from a file. Any suggestions?
Code:
public void Random_Load(object sender, EventArgs e)
{
System.IO.StreamReader wordsIn;
string theWord;
wordCount = 0;
wordArray = new Word[100];
try
{
wordsIn = new System.IO.StreamReader("wordData.txt");
theWord = wordsIn.ReadLine();
while (theWord != "EOF")
{
wordArray[wordCount++] = new Word(theWord);
theWord = wordsIn.ReadLine();
}
wordsIn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error processing input file\n" + ex.Message);
}
Last edited by NCLSEA123; November 14th, 2008 at 12:12 AM.
-
November 14th, 2008, 02:20 AM
#2
Re: Any idea how to randomize this?
I don't get it.
What should be randomized?
Which data?
Data before writing or after reading from file?
What kind of "random" you are looking for?
-
November 14th, 2008, 02:36 AM
#3
Re: Any idea how to randomize this?
Do you want the words from wordData.txt to be read and stored randomly in an array ?
Please make clear your requirement.
-
November 14th, 2008, 04:00 AM
#4
Re: Any idea how to randomize this?
May be this is what your are looking for
Code:
private void button1_Click(object sender, EventArgs e)
{
StreamReader textReader;
try
{
textReader = new System.IO.StreamReader(@"C:\\Words.txt");
string strTemp = textReader.ReadToEnd();
textReader.Close();
string[] words = Regex.Split(strTemp, @"[\s]");
Randomize(words); // here you get the array with random items.
}
catch (Exception ex)
{
MessageBox.Show("Error processing input file\n" + ex.Message);
}
}
static Random rng = new Random();
static void Randomize(string[] list)
{
for (int i = list.Length - 1; i > 0; i--)
{
int swapIndex = rng.Next(i);
if (swapIndex != i)
{
object tmp = list[swapIndex];
list[swapIndex] = list[i];
list[i] = tmp.ToString();
}
}
}
Last edited by MMH; November 14th, 2008 at 04:05 AM.
Regards,
MMH
Rate my post if you find it usefull.
-
November 15th, 2008, 02:48 PM
#5
Re: Any idea how to randomize this?
O.K., so basically, one of my program's goals is to read strings from a specfied textfile randomly, so one random word will appear in a text box during each run. Ultimately, I'm making a hangman style game that will show dashes in the textbox until the user enters the correct letters to make the word appear. The only real change I made to the code was I made the array global.
Code:
public partial class frm1 : Form
{
public static string[] wordArray;
Code:
public void frm1_Load(object sender, EventArgs e)
{
StreamReader wordsIn;
try
{
wordsIn = new System.IO.StreamReader("myData.txt");
string strTemp = wordsIn.ReadToEnd();
wordsIn.Close();
wordArray = Regex.Split(strTemp, @"[\s]");
Randomize(wordArray); // here you get the array with random items.
}
catch (Exception ex)
{
MessageBox.Show("Error processing input file\n" + ex.Message);
}
int tester;
string teststring;
for (int i = 0; i > 0; i++)
{
teststring = wordArray[i];
txtWord.Text = teststring;
}
for (int i = 0; i > 0; i++)
{
tester = wordArray[i].Length;
txtLength.Text = tester.ToString();
}
}
I assume that if this were working properly, the first value of the randomized array would be displayed in the text box, and the length of the string in the other textbox. (I need the length for later when I figure out how many dashes to draw) What dumb thing have I done?
-
November 16th, 2008, 03:18 AM
#6
Re: Any idea how to randomize this?
just read the array normally and randomize which one you choose to be the word chosen?
Code:
string[] items = items;
Random rnd = new Random();
rnd.Next(0, items.Length);
string ChosenWord = items[rnd];
and if you dont want the same word pulled twice just make an array of the numbers already pulled and iterate through that and make sure the rnd dosent equal?
might have misunderstood something, but good luck!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|