i'm pretty new and i was wondering when if i press a butten a random number comes on but when that number is 2 there should be another textbox showing how many times it got 2.
without using if/else or switch
private void btnChoose_Click(object sender, EventArgs e)
{
double number2 = 0;
double When2 = 2;
Random number = new Random();
double chose = number.Next(1,7);
txtnumber.Text = chose.ToString();
txtnumber2.Text = something...
}
Solution: (it's not possible with the number 2 but it is with the number 6)
// global var
int number6 = 0;
private void btnChoose_Click(object sender, EventArgs e)
{
double When6 = 6;
Random number = new Random();
double chose = number.Next(1,7);
txtnumber.Text = chose.ToString();
number6 = Convert.toInt32(number6 + (chose/When6));
txtnumber6.Text = number6.ToString();
}
L.B's note: OP's comment at one of the deleted answer
it's a pretty anoying task and i just need to do this without the if/ese or switch
No array, no dictionary, no Linq, no built-in methods like Convert.ToIn32 etc. Only bit twiddling
Random number = new Random();
int TwosCount = 0;
private void btnChoose_Click(object sender, EventArgs e)
{
int chose = number.Next(1, 7);
TwosCount += ~((chose & 4) >> 2) & ((chose & 2) >> 1) & ~((chose & 1) >> 0);
txtnumber.Text = chose.ToString();
txtnumber2.Text = TwosCount.ToString();
}
PS: You can remove some unnecessary parentheses and write as:
~(chose & 4) >> 2 & (chose & 2) >> 1 & ~(chose & 1);
For a small range of random numbers such as this, (Random.Next(1,7) produces numbers from 1 to 6) you can use an array to keep track of how many times a number has come up. Make sure to declare this array inside of your main class, but outside of your button click method. Also, it doesn't look like you need to use floating point numbers for this job, so I've changed all the double variables to integers.
int[] numberCount = new int[6];
//if you really want to stay with doubles, then simply change the above line to this:
//double[] numberCount = new double[6];
private void btnChoose_Click(object sender, EventArgs e)
{
int number2 = 0;
//double When2 = 2; This line is not needed, so it's been commented out.
Random number = new Random();
int chose = number.Next(1,7);
numberCount[chose - 1]++; // Increment the amount of times we've seen this particular number
txtnumber.Text = chose.ToString();
txtnumber2.Text = numberCount[1].ToString(); // Display the amount of times we've seen the number 2
//Remember, array indexes are 0 based, so index 1 is actually the second number in the array, or number 2.
}
We can use this formula to find our index (Rnd -3)%2
when we subtract 3 from value, if its 2 or lower, it would get negative results as
1-3=-2 and -2%2=0
2-3=-1 and -1%2=-1
and other numbers results would be 0 or 1.
then we can add calculated value by one to start index from 0, now we have an array with three elements which the first element is number of occurrences of 2.
private readonly Random _random=new Random();
private readonly int [] numbers=new int[3];
private void button1_Click(object sender, EventArgs e)
{
var number = _random.Next(1,7);
var chose = number;
chose -= 3; //mod (2-3)=-1
chose %= 2;
chose++;
numbers[chose]++;
txtnumber.Text = number.ToString();
txtNumber2.Text = numbers[0].ToString();
}
Perhaps ;-)
double[] two = { 2 };
int count2 = 0;
private void btnChoose_Click(object sender, EventArgs e)
{
double chose = number.Next(1, 7);
txtnumber.Text = chose.ToString();
count2 = count2 + System.Convert.ToInt32(two.Contains(chose));
txtnumber2.Text = count2.ToString();
}
Random R = new Random();
void RUN()
{
int N = R.Next(2);
Action[] A = new Action[2];
A[0] = new Action(Act1);
A[1] = new Action(Act2);
A[N].Invoke();
}
private void Act2()
{
MessageBox.Show("Cancel");
}
private void Act1()
{
MessageBox.Show("OK");
}
this answer is without if/else/switch
Related
This is my random unique numbers generator I try to create for my cards software. It generates numbers and write into array OK. I have problem with the loop here. when integer i reaches 29, it stops growing and code cycles infinitely and never reaches 30, which would stop the loop.
Without the if statement it works, but it won't fill the range needed.
fixed the code, now works OK, the initial value in array was the problem. now I ged needed 0-29 values
public partial class Form1 : Form
{
int[] rndCards = new int[30];
public Form1()
{
InitializeComponent();
richTextBox1.Text = #"random numbers";
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
rndCards = new int[30];
richTextBox1.Clear();
Random rnd = new Random();
while (i < 30)
{
int cardTest = rnd.Next(0, 30);
while (rndCards.Contains(cardTest))
{
cardTest++;
if (cardTest == 31)
{
cardTest = 1;
}
}
rndCards[i] = cardTest;
i++;
}
i = 0;
while (i < 30)
{
rndCards[i] = rndCards[i] -1;
richTextBox1.Text += rndCards[i] + ", ";
i++;
}
}
}
You problem lies in the simple fact that the array already contains the number 0 when you create it (because each item of an array is initialized to the default value for its member's type) That's why you should start your i from 1 and not zero.
int i = 1;
Alternative Simpler Approach:
You can do this as a simple random number generation:
Random rnd = new Random();
rndCards = Enumerable.Range(0, 30).OrderBy(x => rnd.Next()).ToArray();
foreach(var card in rndCards)
{
// do something
}
rnd.Next(0,30) would return a random number from 0-29.
From the documentation for Random.Next(Int32, Int32):
The Next(Int32, Int32) overload returns random integers that range from minValue to maxValue – 1. However, if maxValue equals minValue, the method returns minValue.
Use int cardText = rnd.Next(0, 31);, and this should solve your issue.
The upper bound is exclusive (C# Random.Next - never returns the upper bound?).
int cardTest = rnd.Next(0, 31);
Its roll the dice application. I want to sum up the dice results and present them to the user. Currently the dice image will change after I click the button "Click to roll the die".
However, When I rolled the dice 1, result will not be add (+0) and when i rolled dice 2, the result will only (+1). I have no idea what's wrong with my code:
public partial class PigForm : Form
{
Image[] diceImages;
int[] dice;
Random roll;
private void rollDieBotton_Click(object sender, EventArgs e)
{
RollDice();
}
private void RollDice()
{
for (int i = 0; i < dice.Length; i++)
{
var currentRoll = roll.Next(0, 6);
dice[i] += currentRoll;
dicePictureBox.Image = diceImages[currentRoll];
playersTotal.Text = String.Format("{0}", dice[i]);
}
}
private void PigForm_Load(object sender, EventArgs e)
{
diceImages = new Image[6];
diceImages[0] = Properties.Resources.Alea_1;
diceImages[1] = Properties.Resources.Alea_2;
diceImages[2] = Properties.Resources.Alea_3;
diceImages[3] = Properties.Resources.Alea_4;
diceImages[4] = Properties.Resources.Alea_5;
diceImages[5] = Properties.Resources.Alea_6;
dice = new int[1] { 0 };
roll = new Random();
}
}
A few remarks on your code:
Why use an array if it will always contain one integer? This also makes the for loop quite useless. Use a plain integer and remove the loop.
The Next() method of the Random class has two parameters. The first is inclusive lower-bound, the second one is exclusive upper-bound. Meaning in your case that the 0 will be a possible number, while 6 will never occur. (MSDN page: Random.Next Method (Int32, Int32))
Here's a slight modification of your code:
public partial class PigForm : Form
{
Image[] diceImages;
int dice;
Random roll;
private void rollDieBotton_Click(object sender, EventArgs e)
{
RollDice();
}
private void RollDice()
{
var currentRoll = roll.Next(1, 7);
dice += currentRoll;
dicePictureBox.Image = diceImages[currentRoll-1];
playersTotal.Text = String.Format("{0}", dice);
}
private void PigForm_Load(object sender, EventArgs e)
{
diceImages = new Image[6];
diceImages[0] = Properties.Resources.Alea_1;
diceImages[1] = Properties.Resources.Alea_2;
diceImages[2] = Properties.Resources.Alea_3;
diceImages[3] = Properties.Resources.Alea_4;
diceImages[4] = Properties.Resources.Alea_5;
diceImages[5] = Properties.Resources.Alea_6;
dice = 0;
roll = new Random();
}
}
var currentRoll = roll.Next(0, 6)
This will generate a random number from 0 to 5, inclusive. You probably want to generate from 1 to 6:
var currentRoll = roll.Next(1, 7)
Reference: Random.Next Method (Int32, Int32)
Edit:
dicePictureBox.Image = diceImages[currentRoll - 1]
First of all, having an integer array for your dice is pointless because you need only one number which also means that you do not need a loop for it because it will never iterate for a second time.
Secondly your random goes from zero to five included and you want it to be from 1 to 6.
Your code with a few edits:
var currentRoll = roll.Next(1, 7);
dice = currentRoll; // there should not be += operator because the result of the next roll will be absurd
dicePictureBox.Image = diceImages[currentRoll - 1]; // -1 because your array is zero-based which means that it starts from 0
That is how the random class really works. The initial value which in your case is 1, is included, but the one in the ending is not, so what you need is 1,7 because it will return a number between 1 and 6 included.
You are allowing the currentRoll variable be anything between [0, 6]. That includes 0 but excludes 6. You should probably change to var currentRoll = roll.Next(1, 7);
Edit for comment: Then for accessing your array values(which is zero indexed) you should subtract one from your roll result.
As others have pointed out, Random.Next(a, b) generates a random number between a inclusive and b exclusive.
While it would be easy to just do
var currentRoll = roll.Next(1, 7);
that would break the array access line you have two lines later.
Instead, your best bet would be to modify the addition line, to do
dice[i] += currentRoll + 1;
Take a look at the documentation of the Random.Next(int, int) method:
http://msdn.microsoft.com/en-us/library/2dx6wyd4%28v=vs.110%29.aspx
There you will find that the lower bound is inclusive and the upper bound is exclusive.
Hence Next(0,6) means that you get a 0, 1, 2, 3, 4 or 5.
I did not exactly understand what your problem is but I did spot something that looks wrong.
Try changing:
dice[i] += currentRoll;
With:
dice[i] += currentRoll+1;
This question already has answers here:
Random number generator in C# - unique values
(5 answers)
Closed 9 years ago.
I'm creating a bingo game and I'm using Random to generate random numbers in an int array but my problem here is that sometimes a number is used again in an index. How can I make the numbers in index unique?
Here is my work:
namespace Bingo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random randNum1 = new Random();
int[] random1 = new int[5];
int qwe = 0;
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
Class1 class1 = new Class1();
class1.checker(this);
if (label1.Text == label2.Text || label3.Text == label4.Text) {
label2.Text = randNum1.Next(1, 15).ToString();
label4.Text = randNum1.Next(1, 15).ToString();
}
if (label5.Text == label1.Text || label5.Text == label2.Text) {
label5.Text = randNum1.Next(1, 15).ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 class1 = new Class1();
class1.SetTwo(this);
for (int i = 0; i < random1.Length; i++)
{
random1[i] = randNum1.Next(1, 15);
label1.Text = random1[0].ToString();
label2.Text = random1[1].ToString();
label3.Text = random1[2].ToString();
label4.Text = random1[3].ToString();
label5.Text = random1[4].ToString();
}
}
}
The problem with looping until you found an unused one is that as the game progresses, you'll take longer and longer to find a valid number. It's theoretically possible that your loop will never end (infinitessimally likely, but still...)
The easiest thing to do is what happens in a real Bingo game. Start from a limited set, and actually remove the item from the set each time you draw. Fill a List or any other dynamic indexed container with your initial possibilities, randomly choose an index from 0 to the size of the list, and then remove the selection out of the list.
This will guarantee that every selection produces a unique result, with no looping.
I figured an illustration of Scott Mermelstein's answer may help:
List<int> AvailableNumbers;
Random random;
private void Form1_Load(object sender, EventArgs e)
{
//Create a list of numbers, 1-14
AvailableNumbers = Enumerable.Range(1, 14).ToList();
random = new Random();
label1.Text = GetNextNumber().ToString();
label2.Text = GetNextNumber().ToString();
label3.Text = GetNextNumber().ToString();
label4.Text = GetNextNumber().ToString();
label5.Text = GetNextNumber().ToString();
}
private int GetNextNumber()
{
//Get a random index within the bounds of AvailableNumbers
var nextIndex = random.Next(0, AvailableNumbers.Count);
var nextNumber = AvailableNumbers[nextIndex];
AvailableNumbers.RemoveAt(nextIndex);
return nextNumber;
}
Another approach is to shuffle the sorted list of numbers:
var numbers = Enumerable.Range(1, 15).OrderBy(i => Guid.NewGuid()).ToArray();
How does this work?
start with a list of integers (Enumerable.Range(1, 15) => [1, 2, 3, ..., 15])
reorder them (OrderBy)
with a "random" index Guid.NewGuid()
Obviously, this will only work, if Guid.NewGuid() doesn't produce consecutive GUIDs, but, I think the default doesn't, since that was a security issue with the first implementations of the GUID algorithm.
(I got the shuffling with GUIDs tip from here:
Randomize a List<T>)
You can use other, more efficient methods to shuffle the "deck", but for Bingo style applications, this should work just fine.
Another idea:
var rand = new Random();
var numbers = Enumerable.Range(1, 15).OrderBy(i => rand.Next()).ToArray();
Since the elements in the starting list are unique, the result is guaranteed to reproduce that property.
everytime you grab a new number, just surround it with this :
int num;
do
{
num = randNum1.Next(1, 15);
}
while(random1.Contains(num))
random1[i] = num;
to guarantee it is unique
Could someone please explain to me why my average keeps coming as 0 when I run my program? I have listed the entire code of my project on here and have literally never used arrays before. Also, is the name of this array mData? I tried reading in my book to determine what to look for in these items and have come up with nothing.
public partial class frmMain : Form
{
private const int mSize = 20;
private int[] mData = new int[mSize];
private int mIndex = 0;
private static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
private frmMain()
{
InitializeComponent();
}
private void btnEnter_Click(object sender, EventArgs e)
{
int num;
num = int.Parse(txtInput.Text);
//store num in the array
mData[mIndex] = num;
mIndex = mIndex + 1;
//check for full array
if (mIndex == mSize)
{
//inform user that array is full
MessageBox.Show("The array is full.");
btnEnter.Enabled = false;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
int n;
for (n = 0; n < mIndex; n++)
listBoxOutput.Items.Add(mData[n]);
}
private void btnAverage_Click(object sender, EventArgs e)
{
int sum = 0;
int average = 0;
if (mIndex == 0)
{
//inform user that array is empty
MessageBox.Show("The array is empty.");
}
//add up the values
for (int i = 0; i < mData.Length; i++)
{
sum += mData[i];
}
//divide by the number of values
average = sum / mSize;
listBoxOutput.Items.Add("The average of the array is: " + average);
}
}
One problem is that you are using ints. If the final value is a decimal less than 1, the int average will store 0. Changing average to a float will solve this. Also, you should not divide by mSize unless you know the entire array is filled. The user could insert one value, but it would be averaged with 19 0s.
since average, sum, and mSize are intergers, when you divide them, the result will be truncated.
average = sum / mSize;
so if sum/mSize is less than 1, average will always be equal to 0
to get average to have decimal points change the declaration to
double average = 0;
and the calculation to
average = (double)sum / (double)mSize;
Array has a built in property to calculate average which returns a decimal value as output. Example is below
int[] integer = new int[] { 1, 2, 3 };
Console.WriteLine(integer.Average().ToString());
Hope this helps.
I have been playing around and wrote this little piece of code. I am trying to flip a coin defined number of times and then count how many tails and heads I am getting. So here it is:
private void Start_Click(object sender, EventArgs e)
{
int headss = 0;
int tailss = 0;
int random2, g;
string i = textBox1.Text;
int input2, input;
bool NumberCheck = int.TryParse(i, out input2);
if (textBox1.Text == String.Empty) // check for empty string, when true
MessageBox.Show("Enter a valid number between 0 and 100000.");
else // check for empty string, when false
if (!NumberCheck) // number check, when false
{
textBox1.Text = String.Empty;
MessageBox.Show("Enter a valid number between 0 and 100000.");
}
else
{
input = Convert.ToInt32(textBox1.Text);
for (g = 0; g < input; g++)
{
Random random = new Random();
random2 = random.Next(2);
if (random2 == 0)
{
headss++;
}
else if (random2 == 1)
{
tailss++;
}
}
}
heads.Text = Convert.ToString(headss);
tails.Text = Convert.ToString(tailss);
}
The problem is that I keep getting problems while displaying the content. It's not even close to display they right result. Any ideas?
EDIT. Solution: move following line 3 lines up :D
Random random = new Random();
Instead of
for (g = 0; g < input; g++)
{
Random random = new Random();
random2 = random.Next(2);
}
Declare a single Random for use throughout:
private Random randomGenerator = new Random();
private void Start_Click(object sender, EventArgs e)
{
// ...
for (g = 0; g < input; g++)
{
random2 = randomGenerator.Next(2);
}
// ...
}
You should use only one Random object to generate good (as good as default Random does) random sequence.
The default constructor for random take the systmem time as seed. Therefore, if you generate lots of them in a short amount of time they will all generate the same sequence of random numbers. Pull the random object out of the loop and this effect will not occur.
With RandomGenerator. This code will count how many times coin has been flipped. It will end with 3 consecutive HEADS.
private RandomGenerator rgen = new RandomGenerator ();
public void run () {
int value = 0;
int total = 0;
while (value != 3) {
String coinFlip = rgen.nextBoolean() ? "HEADS" : "TAILS";
println (coinFlip);
if (coinFlip == "HEADS") {
value+=1;
} else {
value=0;
}
total +=1;
}
println ("It took "+total+" flips to get 3 consecutive heads");
}