So right now, this code can only let you 2 dice ten times, but if i wanted the user to enter any number of dice, how do i do that?
static void Main(string[] args)
{
Random numgen = new Random();
int dice1 = 0;
int dice2 = 1;
for (int roll = 0; roll <=10; roll++)
{
dice1 = numgen.Next(1,7);
dice2 = numgen.Next(1,7);
Console.WriteLine(dice1 + "\t" + dice2);
}
Console.ReadLine();
}
Since it doesn't seem to look like you need to save any of your dice rolls, but rather just output them to the screen I would omit the dice ints.
public void RollDice(int NumberOfDice)
{
Random numgen = new Random();
for (int roll = 0; roll <= 10; roll++)
{
for (int i = 0; i < NumberOfDice; i++)
{
Console.WriteLine(numgen.Next(1, 7) + "\t" + numgen.Next(1, 7));
}
}
Console.ReadLine();
}
Related
Beginner to programming & have been assigned a heads or tails coin flip project.
I've figured out how to make it randomly generate the number of requested flips and print out the answers. However, I'm supposed to have it add up how many times the user's input came up into the correctCount variable and I can't find anywhere how to do it. I've tried searching on here and different sites from searching throughout Google. I assume I need to take their string input and convert it somehow, like if result == string, then correctCount + 1 basically, but can't figure out how to make that happen since you can't do that with int & string. Any info or hints would be very helpful - thank you!
class Coin
{
static void Main(string[] args)
// UserInput
{
Console.Write("Guess which will have more: heads or tails?");
string headsOrTailsGuess = Console.ReadLine() + "\n";
Console.Write("\n" + "How many times shall we flip the coin? ");
int numberOfFlips = int.Parse(Console.ReadLine() + "\n");
// Declare variables
int correctCount = 0;
int heads = 0;
int tails = 1;
Random rand = new Random();
for (int i = 0; i < numberOfFlips; i++)
{
int result = rand.Next(0, 2);
if (result == 0)
{
Console.WriteLine("Heads!");
}
else
{
Console.WriteLine("Tails!");
}
}
Console.WriteLine("Your guess, " + headsOrTailsGuess + "came up " + correctCount + " time(s).");```
As PMF mentioned, you have to not only receive user choice, but also analyse it.
A simple comparison should be more that enough here, although you might want to add some validation to user input.
static void Main(string[] args)
// UserInput
{
int choice; //variable for parsed user choice
Console.Write("Guess which will have more: heads or tails?");
string headsOrTailsGuess = Console.ReadLine() + "\n";
if(headsOrTailsGuess.ToLower().Trim() == "heads"){ //here you look if its heads
choice = 0;
}
else{ //we can write additional if here to avoid other options from counting as tails
choice = 1;
}
Console.Write("\n" + "How many times shall we flip the coin? ");
int numberOfFlips = int.Parse(Console.ReadLine() + "\n");
// Declare variables
int correctCount = 0;
int heads = 0;
int tails = 1;
Random rand = new Random();
for (int i = 0; i < numberOfFlips; i++)
{
int result = rand.Next(0, 2);
if (result == 0)
{
Console.WriteLine("Heads!");
}
else
{
Console.WriteLine("Tails!");
}
if(result == choice){ //comparing result to parsed choice and incrementing the counter
correctCount++;
}
}
Console.WriteLine("Your guess, " + headsOrTailsGuess + "came up " + correctCount + " time(s).");
}
Here's another way to do it using Linq.
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
// UserInput
{
Console.Write("Guess which will have more: heads or tails?");
string headsOrTailsGuess = Console.ReadLine();
Console.Write("\n" + "How many times shall we flip the coin? ");
int numberOfFlips = int.Parse(Console.ReadLine() + "\n");
// Declare variables
List<string> resultList = new List<string>();
Random rand = new Random();
for (int i = 0; i < numberOfFlips; i++)
{
int result = rand.Next(0, 2);
resultList.Add(result == 1? "Heads!" : "Tails!");
}
var guessCount = resultList.Where(a=>a.ToUpper().Contains(headsOrTailsGuess.ToUpper())).Count();
resultList.ForEach( a=> Console.WriteLine(a));
Console.WriteLine("Your guess, " + headsOrTailsGuess + " came up " + guessCount + " time(s).");
Console.WriteLine(headsOrTailsGuess);
}
}
}
using System;
Console.WriteLine("Guess which will have more: heads or tails?");
var headsChosen = false;
var input = string.Empty;
do
{
input = Console.ReadLine();
headsChosen = input.ToLower() == "heads";
} while (input.ToLower() != "heads" && input.ToLower() != "tails");
Console.WriteLine("How many times shall we flip the coin?");
var flips = 0;
do
{
} while (!int.TryParse(Console.ReadLine(), out flips));
var rnd = new Random();
var heads = 0;
var tails = 0;
for (int i = 0; i < flips; i++)
{
if (rnd.Next(0, 2) == 0)
{
Console.WriteLine("Heads!");
heads++;
}
else
{
Console.WriteLine("Tails!");
tails++;
}
}
var userGuessed = (headsChosen && heads > tails) || (!headsChosen && tails > heads);
Console.WriteLine($"You {(userGuessed ? "guess" : "loose")}!");
I made a program to generate 7 random numbers for a lottery using a array. I have generated a random number between 1, 50 but every number shows in order and not on the same line. I would also like to store the auto generated numbers in a array to use. I am not sure how to fix this any help would be appreciated
static void AutoGenrateNumbers()
{
int temp;
int number = 0;
int[] lotto = new int[7];
Random rand = new Random();
for (int i = 0; i <= 50; i++)
{
number = 0;
temp = rand.Next(1, 50);
while (number <= i)
{
if (temp == number)
{
number = 0;
temp = rand.Next(1, 50);
}
else
{
number++;
}
}
temp = number;
Console.WriteLine($"the new lotto winning numbers are:{number}Bonus:{number}");
}
}
Is this what you need?
static void AutoGenrateNumbers()
{
int temp;
int[] lotto = new int[7];
Random rand = new Random();
for (int i = 0; i < 7; i++)
{
temp = rand.Next(1, 50);
lotto[i]= temp;
}
Console.Write($"the new lotto winning numbers are: ");
for (int i = 0; i < 6; i++)
{
Console.Write(lotto[i]+" ");
}
Console.Write($"Bonus:{lotto[6]}");
}
edit: if you want the numbers to be unique:
static void AutoGenrateNumbers()
{
int temp;
int[] lotto = new int[7];
Random rand = new Random();
for (int i = 0; i < 7; i++)
{
do
{
temp = rand.Next(1, 50);
}
while (lotto.Contains(temp));
lotto[i]= temp;
}
Console.Write($"the new lotto winning numbers are: ");
for (int i = 0; i < 6; i++)
{
Console.Write(lotto[i]+" ");
}
Console.Write($"Bonus:{lotto[6]}");
}
A better way to do this is just to generate all the numbers 1-50, shuffle them and then just take 7. Using Jon Skeet's Shuffle extension method found here:
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
T[] elements = source.ToArray();
for (int i = elements.Length - 1; i >= 0; i--)
{
int swapIndex = rng.Next(i + 1);
yield return elements[swapIndex];
elements[swapIndex] = elements[i];
}
}
Now your code is very simple:
static void AutoGenrateNumbers()
{
var lotto = Enumerable.Range(0, 50).Shuffle(new Random()).Take(7);
Console.WriteLine("the new lotto winning numbers are: {0}", string.Join(",", lotto));
}
Fiddle here
Just to add to the existing answers tried to do that in one LINQ statement:
static void Main(string[] args)
{
var rand = new Random();
Enumerable
.Range(1, 7)
.Aggregate(new List<int>(), (x, y) =>
{
var num = rand.Next(1, 51);
while (x.Contains(num))
{
num = rand.Next(1, 51);
}
x.Add(num);
return x;
})
.ForEach(x => Console.Write($"{x} "));
}
The result is something like:
34 24 46 27 11 17 2
This is my code:
static void Main(string[] args)
{
Console.Write("How many tests would you like to do? 1 to 10: ");
int tests = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
}
can someone help me out with my code please? i have no idea what im doing
Thanks
Make an array int[] counts = new int[13] and just use counts[total]++;; at the end, loop over it:
for(int i = 2 ; i <= 12 ; i++)
// etc
(note: you could use an array of 11 items and constantly handle the off-by-two, but... it probably isn't worth it)
Something like:
static void Main()
{
Console.WriteLine("Investigation 1");
Console.WriteLine("===============");
Console.WriteLine();
Console.Write("How many sets of tests? 1 to 10: ");
int sets = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Random r = new Random();
int[] counts = new int[13];
for (int ctr = 0; ctr < 36 * sets; ctr++)
{
int a = r.Next(1, 7), b = r.Next(1, 7), total = a + b;
Console.WriteLine($"Roll {(ctr + 1)}: {a} + {b} = {total}");
counts[total]++;
}
Console.WriteLine("=======================");
Console.WriteLine();
Console.WriteLine("Total Expected Actual");
Console.WriteLine("===== ======== ======");
for(int i = 2; i <= 12; i++)
{
var expected = sets * (6 - Math.Abs(7 - i));
Console.WriteLine($" {i} {expected} {counts[i]}");
}
}
For a histogram:
var maxCount = counts.Max(); // needs "using System.Linq;" at the top
for (int i = 2; i <= 12; i++)
{
var width = ((Console.WindowWidth - 10) * counts[i]) / maxCount; // make it proportional
Console.WriteLine($"{i}\t{new string('*', width)}");
}
I am fairly new at programming and am currently picking up C#.
What I have is two dice rolled at random for 50 rolls
Both dice totals added together to give a sum for each roll.
My question is, how can I create a frequency table that will count the amount of times a certain number is rolled at the end of 50 dice rolls.
Below is what I am trying to achieve. Tally chart of each number rolled (from both dice)
1: |||
2: ||||
3: ||||||
4: |||||
5: ||||||||
6: ||
Here is my code so far,
class Program
{
const int DICE_ROLLS = 51;
static void Main(string[] args)
{
Random random = new Random();
int[] firstDice = new int[DICE_ROLLS];
int[] secondDice = new int[DICE_ROLLS];
int diceSum = 0;
for (int roll = 1; roll <= 50; roll++)
{
firstDice[roll] = GenerateNumber(random);
secondDice[roll] = GenerateNumber(random);
diceSum = firstDice[roll] + secondDice[roll];
Console.WriteLine("ROLL {0}: {1} + {2} = {3}", roll, firstDice[roll], secondDice[roll], diceSum);
}
Console.WriteLine();
Console.WriteLine("~~~~~~~~~~~~~~~~");
Console.WriteLine("Number Frequency");
}
static int GenerateNumber (Random random)
{
return random.Next(1, 7);
}
}
Try this:
const int DICE_ROLLS = 51;
static void Main(string[] args)
{
Random random = new Random();
int[] firstDice = new int[DICE_ROLLS];
int[] secondDice = new int[DICE_ROLLS];
int[] count = new int[6];
int diceSum = 0;
for (int roll = 0; roll <= DICE_ROLLS - 1; roll++)
{
firstDice[roll] = GenerateNumber(random);
secondDice[roll] = GenerateNumber(random);
diceSum = firstDice[roll] + secondDice[roll];
Console.WriteLine("ROLL {0}: {1} + {2} = {3}", roll + 1, firstDice[roll], secondDice[roll], diceSum);
}
Console.WriteLine();
Console.WriteLine("~~~~~~~~~~~~~~~~");
Console.WriteLine("Number Frequency");
var allRolls = firstDice.Concat(secondDice).ToList();
for(var i = 1; i <= 6; i++)
{
Console.Write(i + ": ");
for(var j = 0; j < allRolls.Count(c => c == i); j++)
Console.Write("|");
Console.WriteLine();
}
}
static int GenerateNumber (Random random)
{
return random.Next(1, 7);
}
Gives:
~~~~~~~~~~~~~~~~
Number Frequency
1: ||||||||||||||||||||
2: |||||||||||
3: ||||||||||||||||||
4: ||||||||||||||||
5: |||||||||||||||||||||
6: ||||||||||||||||
Assuming 6 sided dice, a Dictionary might be a good option.
var rolls = new SortedDictionary<int, int> { {1,0}, {2,0}, {3,0}, {4,0}, {5,0}, {6,0} };
for(int i = 0; i < 50; i++)
{
var first = Random.Next(1, 7);
var second = Random.Next(1, 7);
rolls[first]++;
rolls[second]++;
// Rest of rolling display code
}
// Header display code
foreach(var roll in rolls)
{
Console.Write("{0}: ", roll.Key);
for(int i = 0; i < rolls.Value; i++)
{
Console.Write("|");
}
Console.WriteLine();
}
You could even easily customize it based on different sided dice by generating the initial dictionary values based on a variable like
int sides = 12;
var rolls = new SortedDictionary<int, int>();
foreach(var value in Enumerable.Range(1, sides))
{
rolls.Add(value, 0);
}
The you could replace the Random.Next(1, 7) with Random.Next(1, sides) and have a pretty generic program for rolling dice and counting their frequency.
This works:
var rnd = new Random();
Console.WriteLine(
String.Join(
Environment.NewLine,
Enumerable
.Range(0, 50)
.Select(n => rnd.Next(1, 7))
.ToLookup(x => x)
.Select((xs, n) =>
String.Format("{0}: {1}", n + 1, new String('|', xs.Count())))));
I get this (for example):
1: |||||||
2: |||||||
3: |||||||||||
4: |||||||||
5: ||||||||
6: ||||||||
I wrote this code to order any set of numbers from biggest to smallest, but for whatever reason, the output always has a zero at the end. My question is, where did it come from? (new to coding)
Console.WriteLine("Please enter set of numbers");
int input = Convert.ToInt16(Console.ReadLine());
int counter= 1;
int temp1;
int temp2;
int[] array = new int[input+1];
for (int i = 0; i <=input-1; i++)
{
Console.WriteLine("Please enter entry number " + counter);
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
counter++;
}
for (int x = 0; x <= input; x++)
{
for (int i = 1; i <=input; i++)
{
if (array[i - 1] <= array[i])
{
temp1 = array[i - 1];
temp2 = array[i];
array[i - 1] = temp2;
array[i] = temp1;
}
}
}
Console.WriteLine();
for (int i = 0; i<=input; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadLine();
You're inconsistent between whether you're trying to handle input + 1 or input elements. For example:
int[] array = new int[input+1];
for (int i = 0; i <=input-1; i++)
{
Console.WriteLine("Please enter entry number " + counter);
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
counter++;
}
You're creating an array with input + 1 elements, but only populating input of them.
In general, it's much more common to use exclusive upper boundaries for loops. For example:
int[] array = new int[input];
for (int i = 0; i < input; i++)
{
// No need for the counter variable at all
Console.WriteLine("Please enter entry number " + (i + 1));
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
}