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;
Related
I want to check if there is same value in the array or not as I mentioned in the title. And if there is, I want to pass that value and check another random value to add to listbox.
In my form, there is 2 textBox, 1 listbox and 1 button. When button is clicked, listbox has to show random numbers up to sum of textbox1 and textbox2. For instance;
5 entered from textbox1 and 10 entered from textbox2. Sum is of course 15 and listbox has to show 15 random numbers but those numbers have to be different from each other.
I wrote something like that and used Contains method to check if there is same value or not. But the program froze and didn't give any error.
int a, b;
Random rnd = new Random();
int[] array;
private void button1_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(textBox1.Text);
b = Convert.ToInt32(textBox2.Text);
int c = a + b;
array = new int[c];
for (int i = 0; i < array.Length; i++)
{
int number = rnd.Next(c);
foreach(int numbers in array)
{
if (array.Contains(numbers))
{
i--;
}
else
{
array[i] = number;
listBox1.Items.Add(array[i]);
}
}
}
I also did it without foreach(Only Contains part I mean). Also didn't work. I wrote in "else";
array[i] += number;
it also didn't work.
I would be very appreciated if you help me. Thanks in advance.
instead of a for loop, use a while loop:
int = 0;
while(i<c)
{
int random rnd.Next(c);
if(!array.Contains(random))
array[i++] = random;
}
you may also create a list of numbers from 1-15 and then shuffle them (as your random function will create only random numbers from 1-15 just random):
array = Enumberable.Range(0,c).OrderBy(x => rnd.Next()).ToArray();
The above code is much faster, because imagine that we have generated 14 random numbers and only one number (5 for instance) left, it has to go through loop several times so that finally random number that is generated equals to 5, but in the above code there is no need to check that, we just have all numbers and then we shuffle it.
You can try to use do...while instead of for loop
Random.Next get the value from 0 to c - 1, so rnd.Next(c + 1); need to add 1 otherwise, the loop will not be stopped.
var array = new int[c];
int number;
for (int i = 0; i < array.Length; i++)
{
do
{
number = rnd.Next(c + 1);
} while (array.Contains(number));
array[i] = number;
listBox1.Items.Add(array[i]);
}
You basically need to shuffle your data. Create a collection with all values:
var temp = Enumerable.Range(0, c);
Now order it by random
temp = temp.OrderBy(_ => rnd.Next());
Now you can add temp to your listBox
Or, as single line:
listBox1.Items.AddRange(Enumerable.Range(0, c).OrderBy(_ => rnd.Next()));
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);
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
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
I have two issues that I'm not sure how to fix.
diceThrow() is supposed to randomly roll a die and come up with an answer 1-6 multiple times, but only comes up with one 1-6 answer and only does that. i.e. (6, 6, 6, 6, 6, 6, etc)
and for rollDice(), I'm not sure if I just poorly defined "i" or maxRolls, but it should be that when i > maxRolls, the program should end and reset.
Any advice on how to fix either of these is greatly appreciated, thanks!
//somewhere else in code
int maxRolls = RollsNumber();
int throwresult = diceThrow();
int i;
//*******************************
private void rollButton_Click(object sender, EventArgs e)
{
rollDice();
wagerTextBox.Text = null;
wagerTextBox.Text = scoreTextBox.Text;
diceThrow();
MessageBox.Show(Convert.ToString(throwresult));
if (maxRolls < i)
{
MessageBox.Show("You got too greedy.");
//reset the form
}
}
// Decides the maximum number of rolls before the player loses
static public int RollsNumber()
{
Random rolls = new Random();
return rolls.Next(1, 10);
}
// Throws the dice
static public int diceThrow()
{
Random dice = new Random();
return dice.Next(1, 7);
}
private void rollDice()
{
for (i = 0; i <= maxRolls; i++)
{
int wager = Convert.ToInt32(wagerTextBox.Text);
int score = wager * 100;
scoreTextBox.Text = Convert.ToString(score);
}
}
}
}
You are using same seed with the Random.
As msdn states in Random class
The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random.
A simple way in your case is to not create new Random each time.
// Throws the dice
static Random diceRandom = new Random();
static public int diceThrow()
{
return diceRandom .Next(1, 7);
}