This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Random number generator not working the way I had planned (C#)
Hi,
Below is the complete code for a small app I am writing - it returns an ArrayList of alphanumeric codes. The problem I have is that when 'stepping' through the code the e.Result is returned correctly with each item being different. However, if I let the app run without the breakpoints I just get multiple instances of the same variable back:
public partial class AlphaNum : Form
{
public AlphaNum()
{
InitializeComponent();
// Initialise BackGroundWorker Reporting
backgroundWorker1.WorkerReportsProgress = true;
// Initialise BackGroundWorker Cancel
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
button1.Enabled = false;
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
public string RandomString(Random r, int len)
{
string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder sb = new StringBuilder();
while ((len--) > 0)
sb.Append(str[(int)(r.NextDouble() * str.Length)]);
return sb.ToString();
}
private string generateCode()
{
Random rnd = new Random();
int length = int.Parse(stringLength.Text);
string code = "";
code = RandomString(rnd, length);
return code;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int quantity = int.Parse(quantityRequired.Text);
ArrayList codes = new ArrayList();
string myText = "";
for (int a = 0; a < quantity; a++)
{
myText = generateCode();
codes.Add(myText);
backgroundWorker1.ReportProgress(((100 / quantity) * a));
}
e.Result = codes;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage; //update progress bar
//Console.WriteLine(time);
//in this example, we log that optional additional info to textbox
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
ArrayList codes = new ArrayList();
codes = (ArrayList)e.Result;
for (int i = 0; i < codes.Count; i++ )
{
outputText.Text += codes[i].ToString();
outputText.AppendText(Environment.NewLine);
}
}
}
}
Can anyone explain why the result is okay when I effectively slow the process down by stepping my way through but incorrect when it runs without being stopped/slowed down?
Many Thanks
The loop in backgroundWorker1_DoWork is so fast that the Random object generated in generateCode is always seeded with the same value, thus producing the same values. Don't recreate the Random object but assign one to an instance variable in the constructor of your class and only use that one.
You shouldn't create a new Random object every time you need a random number.
Random rnd = new Random();
uses the current time to initialize the LFSR, when the program runs at full speed, the same "current time" gets used many times in a row.
Related
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 6 years ago.
When I press the click event to add array values, it gives me an out of index exception error. I'd like to know why it's happening and how to fix it, thanks.
using System;
using System.Windows.Forms;
namespace ArrayTestCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double total = 0;
double average = 0;
double scoreInput = 0;
int countOfExams = 0;
double[] examScores;
double count = 0;
//field array declare
private void enterBtn_Click(object sender, EventArgs e)
{
countOfExams = int.Parse(numOfExamsTextBox.Text);
examScores = new double[countOfExams];
examLabel.Text = "Exam 1: ";
label1.Visible = false;
numOfExamsTextBox.Visible = false;
enterBtn.Visible = false;
examLabel.Visible = true;
examScoresTextBox.Visible = true;
addBtn.Visible = true;
}
private void addBtn_Click(object sender, EventArgs e)
{
examLabel.Text = "Exam " + (countOfExams +1) + ":";
examScores[countOfExams] = double.Parse(examScoresTextBox.Text);
countOfExams++;
examScoresTextBox.ResetText();
examScoresTextBox.Focus();
if(countOfExams >= examScores.Length)
{
MessageBox.Show("hoise vaya");
}
}
private void clearBtn_Click(object sender, EventArgs e)
{
Array.Clear(examScores, 0, examScores.Length);
ClearAll();
}
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
public void ClearAll()
{
examLabel.Text = "Exam:";
examScoresTextBox.ResetText();
numOfExamsTextBox.ResetText();
}
private void calcBtn_Click(object sender, EventArgs e)
{
}
}
}
In the first line of your enterBtn_Click,
countOfExams = int.Parse(numOfExamsTextBox.Text);
I suppose you are using countOfExams as the total length of the array.
Later in the second line of your addBtn_Click,
examScores[countOfExams] = double.Parse(examScoresTextBox.Text);
countOfExams++;
I suppose you are using countOfExams to track the actual number of exams. Because countOfExams is already set to the length, it results in OutOfRangeException.
Thus, I suggest you to use another variable (e.g. a local variable) for the total length of the array,
var size = int.Parse(numOfExamsTextBox.Text);
examScores = new double[size];
double[] examScores; has been defined as array, which was never initialized if you are not calling enterBtn_Click first
Ideally,
You should first initialize/ check if it is initialized it by
double[] examScores = new double[<length>];
also, the counter starts from 0 and not 1
If you are not sure about the length or capacity, use List
I am building a program that on a button click displays a random line from a text file into a text box.
I am only a beginner at C# so I am not sure where I have gone wrong.
private void startButton_Click(object sender, EventArgs e)
{
int lineCount = File.ReadAllLines(#"D:...\QUESTIONS.text").Length;
Random rnd = new Random();
int randomLineNum = rnd.Next(lineCount);
int indicator = 0;
using (var reader = File.OpenText(#"D:...\QUESTIONS.text"))
{
while (reader.ReadLine() != null)
{
if (indicator == randomLineNum)
{
questionBox.Text = reader;
break;
}
indicator++;
}
}
}
Can you also help me figure out where this code is supposed to go.
Thank you for your help in advance! : )
you can remove many complexities in your code.
private Random r = new Random();
private void startButton_Click(object sender, EventArgs e)
{
var lines = File.ReadAllLines(#"D:...\QUESTIONS.text");
questionBox.Text = lines[r.Next(lines.Length)];
}
defining a random variable outside the function scope and reuse it
every time is a known best practice.
why reading the file twice? you are reading it once with ReadAllLines and then again with an StreamReader
This should do it:
private void startButton_Click(object sender, EventArgs e)
{
var lines = File.ReadAllLines(#"D:...\QUESTIONS.text");
int lineCount = lines.Length;
Random rnd = new Random();
int randomLineNum = rnd.Next(lineCount);
questionBox.Text = lines[randomLineNum];
}
There is no need to read the same file twice, so keep the lines in a local variable to access it later.
im pretty new to C# and i want to make a cardgame. What i have is a list of cards (strings) with names like c6, s3, h11, d13 where the character represents the colour and the number represents the value. When pressing a button the program takes a random string from the list and displays it in a textbox. From there the point of the game is to guess if the next random card will have a higher value or a lower value then the previous card.
What i want to do is to take the string from the textbox and turn it into a int so that i can compare the value of the previous card with the new one. Only problem is how do i get rid of the c in c6 so i can convert it by using parse.
This is my code.
public partial class MainWindow : Window
{
static Random rndmlist = new Random();
Random rndm = new Random();
List<string> deck = new List<string>();
int score = 0;
public MainWindow()
{
InitializeComponent();
}
private void test_Click(object sender, RoutedEventArgs e)
{
//disregard this
foreach (string j in deck)
{
testbox.Text += j + ", ";
}
}
private void btnstart_Click(object sender, RoutedEventArgs e)
{
//this is where i add all the cards to the list
for (int i = 1; i <= 13;)
{
deck.Add("c" + i);
i++;
}
for (int i = 1; i <= 13; )
{
deck.Add("s" + i);
i++;
}
for (int i = 1; i <= 13; )
{
deck.Add("h" + i);
i++;
}
for (int i = 1; i <= 13; )
{
deck.Add("d" + i);
i++;
}
}
private void btnbegin_Click(object sender, RoutedEventArgs e)
{
//this is where i take a random card from the list and display it in textBox2
int r = rndmlist.Next(deck.Count);
textBox2.Text = ((string)deck[r]);
//disregard this
testbox.Text += ((string)deck[r]) + ", ";
deck.Remove((string)deck[r]);
}
private void btnhigh_Click(object sender, RoutedEventArgs e)
{
//this is where i want to compare the cards.
}
}
Thank you in advance for reading this. (:
I'd better create a class Card, which represents a card, with 2 properties: Color and Number and implemented method Card.ParseFromString()
Try this,
string SubString = MyString.Substring(1);
But take care if the string is empty, it is an error case.
Assuming there will always be one (and only one) character before the number, you can simply do this:
string numberAsString = "c2".Substring(1);
And to make that an int:
int number = Int32.Parse(numberAsString);
You can use Regex to replace all alpha characters eg
string result = Regex.Replace(myString, #"[a-zA-Z\s]+", string.Empty);
string str = "c12";
var noChars = str.SubString(1); // take a new string from index 1
var number = Int32.Parse(noChars);
I need help with timer and List.
List consist of collection of string say 5 or 6 at a time. Now, I want to display string one on label1 and it should wait for 5s and then display string 2 on label1. I have timer control and I am specifying my code in timer_tick event.
private void timer1_Tick(object sender, EventArgs e)
{
string[] myStatus = myStatusCollection.ToArray();
int length = myStatus.Length;
for (int i = 0; i < length; i++)
{
string _myStatus = myStatus[i];
//label1.ResetText();
MessageBox.Show("Twitter Status =" + _myStatus);
//label1.Text = _myStatus;
//label1.Visible = true;
}
}
I have specify, Elapse = true and interval = 5000 but still I am not able to display one string at a time. In fact, I am getting last string only. I want to rotate the strings all time.
Can anyone help me.
That's because you're looping through all the strings each time the timer event fires.
Store your index in a private variable and use that instead.
private int _index = 0;
private void timer1_Tick(object sender, EventArgs e)
{
string[] myStatus = myStatusCollection.ToArray();
string _myStatus = myStatus[_index];
//label1.ResetText();
MessageBox.Show("Twitter Status =" + _myStatus);
//label1.Text = _myStatus;
//label1.Visible = true;
if(_index == (myStatus.Length - 1))
_index = 0;
else
_index++;
}
Well it is doing just what you told it to. However, what you told it to do is not what you meant for it to do. Try this.
public class Form1 : Form {
private string[] statuses = { "A", "B", "C", "D", "E" }; // Init with proper values somewhere
private int index = 0;
private void OnTimerTick(object sender, EventArgs e) {
string status = statuses[index];
index++;
if (index == statuses.Length) { // If index = Array.Length means we're
// outside bounds of array
index = 0;
}
}
}
I'd create an int outside of the Tick to hold your position. Make sure you reset it back to 0 when you restart the process.
int MyPosition = 0;
private void timer1_Tick(object sender, EventArgs e)
{
string[] myStatus = myStatusCollection.ToArray();
int length = myStatus.Length;
if((MyPosition + 1) > length)
{
//Index Out of Range
}
else
{
string _myStatus = myStatus[MyPosition];
label1.Text = _myStatus
}
MyPosition++;
}
Hi People I'm newbie in the C# world and I'm having a problem. I have done an array in the Form_Load method of my program, but I need to access the array in a picture_box method like this:
private void Form2_Load(object sender, EventArgs e)
{
//In this method we get a random array to set the images
int[] imgArray = new int[20];
Random aleatorio = new Random();
int num, contador = 0;
num = aleatorio.Next(1, 21);
imgArray[contador] = num;
contador++;
while (contador < 20)
{
num = aleatorio.Next(1, 21);
for (int i = 0; i <= contador; i++)
{
if (num == imgArray[i])
{
i = contador;
}
else
{
if (i + 1 == contador)
{
imgArray[contador] = num;
contador++;
i = contador;
}
}
}
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(#"C:\Users\UserName\Desktop\MyMemoryGame\" + imgArray[0] + ".jpg");
}
But I only get the error: Error 1 The name 'imgArray' does not exist in the current context
You need to define int[] imgArray at the class level (outside of Form2_Load) rather than inside it. Otherwise the "scope" of that variable is limited to that function. You will need to knock off the first "int[]" part in Form2_Load to prevent you from just declaring a new variable.
For example:
public class MyClass
{
private int[] myInt;
public void Form2_Load(...) {
myInt = ...;
}
}
The error means exactly what it says.
You've declared the array in the scope of the Form2_Load function. Outside of it, it will not exist.
To do what you're trying to achieve, add a private array to the form itself.
private int[] _imgArray = new int[20];
private void Form2_Load(object sender, EventArgs e)
{
//Setup the imgArray
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//_imgArray is now available as its scope is to the class, not just the Form2_Load method
}
Hopefully that helps.