after reading some C# tutorials I decided to make a minigame of Heads/Tails. It generates a random number, 0 or 1, and writes out the result. Using a loop I repeat this a thousand times. The problem is that it would only write "heads" or "tails" depending on whether 0 or 1 gets generated more. For example if there are 535 "0s" and 465 "1s" it would only write down "Heads". Here is my code:
//variables
int count = 0;
int tails = 0;
int heads = 0;
while(count < 1000)
{
Random rnd = new Random();
int result = rnd.Next(0,2);
if(result == 1)
{
Console.WriteLine("Tails!");
tails = tails + 1;
count = count + 1;
}
else if(result == 0)
{
Console.WriteLine("Heads!");
heads = heads + 1;
count = count + 1;
}
}
Console.WriteLine("Heads = " + heads + "Tails = " + tails + " Counts = " + count);
Console.ReadLine();
Try moving Random rnd = new Random(); outside of the while loop:
Random rnd = new Random();
while (count < 1000)
//...
The problem is that there are no true random numbers in computers; they all work off of a list of previously-generated random numbers. Since you are instantiating Random every loop, you're essentially picking the same start seed every time. By using just one instance of Random, created outside of the loop, then your application will truly behave as if the numbers are being generated randomly.
EDIT: To echo what Solal Pirelli said in the comments, the instance of Random is actually being seeded using the current computer system's time (if you don't give it any seed value in the constructor); however, since the loop iterations are happening so quickly, each instance created for each loop iteration has the same seed.
EDIT #2: As CalebB pointed out, it's also a good practice to provide your own seed to your Random instance via its other constructor. I would suggest using the hash value from a GUID:
Random rnd = new Random(Guid.NewGuid().GetHashCode());
This essentially guarantees that each of your instances of Random will always be seeded differently, even if you create new instances in quick succession. I say essientially because, while statistically very VERY low in probability, there is some chance that two GUID values could be the same.
Fixed! :
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
Random rand = new Random();
Console.WriteLine(String.Join("\n",Enumerable.Repeat(0, 1000).Select(i => rand.Next(0,2) == 1 ? "Tails" : "Heads").GroupBy(i=>i).Select(g=> g.Key + " " + g.Count())));
Console.ReadLine();
}
}
}
Related
I am a beginner at C# and I have a homework assignment where I have to write a dice roll simulator.
The user can chose a maximum of 5 die and then as they roll each one the sum is added up with the one rolled previously. At the end the number of rolls and the the total sum is presented.
However, if the user rolls a 6 then this result is not added to the sum but the die is rolled twice more and then the sum is added.
I have a few points where I have gotten stuck and/or would like to know a better way of writing it. I have included a snippet below.
Below I have stated my Random class every time the dice rolls but is it possible for me to do this only once and then use throughout my code?
Is there a simple way to write the part with what happens if you roll a 6? I mean I guess I could write another if statement every time the die rolls but seems that this would very long?
if (intnumberofdie == 1)
{
Random rnd1 = new Random();
int dice1 = rnd1.Next(1, 7);
Console.WriteLine("You have rolled " + dice1);
else if (intnumberofdie == 2)
{
Random rnd1 = new Random();
int dice1 = rnd1.Next(1, 7);
Console.WriteLine("The first die rolled " + dice1);
Console.ReadKey();
Random rnd2 = new Random();
int dice2 = rnd2.Next(1, 7);
Console.WriteLine("The second die has rolled " + (dice2));
Console.ReadKey();
Console.WriteLine("The total sum so far is " + (dice1 + dice2));
}
Give this one a go:
var rnd1 = new Random();
var rollsRemaining = 6;
var sum = 0;
while (rollsRemaining > 0)
{
int dice1 = rnd1.Next(1, 7);
Console.WriteLine("You have rolled " + dice1);
if (dice1 == 6)
{
rollsRemaining += 2;
}
else
{
sum += dice1;
rollsRemaining -= 1;
}
}
Console.WriteLine("The sum is " + sum);
Since you are a beginner I'll try to explain a few things first.
If you have a number of times you have to do the same thing, you should never handle each case yourself. This means you shouldn't use the if else if else.. but instead use a loop like for or while.
I have written a short function which should do what you want.
private int GetNumber(int amountDie) {
if (amountDie <= 0 || amountDie > 5) return 0;
int total = 0;
for (int i = 0; i < amountDie; i++)
{
int roll = rnd.Next(1, 7);
if (roll == 6)
{
int specialRoll1 = rnd.Next(1, 7);
int specialRoll2 = rnd.Next(1, 7);
total += specialRoll1;
total += specialRoll2;
}
else {
total += roll;
}
}
return total;
}
Now some more explaining:
Since you said that you can't have more than 5 die, we will first check if the value given was valid, otherwise we'll just return 0 (there are better things to do in this case but for simplicity we'll just say they get back 0).
After that we declare a total to keep track of how many points we have to return.
Then we start the loop. For each dice, we roll once (first line in the for-construct).
Since we need to check if the user rolled a 6, we have this if there. Inside it we roll two die and add both results to the total. Note, that for these two rolls 6 is just like any other number and not handled differently (if you need that, it would be a bit more complicating).
If the user didn't roll a 6, we just add whatever they rolled to the total.
After we've done that for all die we simply return the total we got.
You'll see that if you just copy paste this code, it will not compile. The reason for that is that you are missing a Random-object. In your code you always created a new one everytime you rolled. This is a problem since the Random class isn't as random as you might think and you should always create just one for many random numbers.
Thats why I added this at the top of my class:
private static readonly Random rnd = new Random();
This random-object is only created once and then used for every die. This gives you a lot more randomness and prevents that every user always rolls the same value.
I hope this shows you how you can create a method like this. Of course you can add the console-output etc like you did in your code.
Im new to programming and Im currently attempting to make a dice program, where the user can input how many throws they would like to do and then a list will display how many throws it took to get a specific number, in this case that number is 6 (later on I'd like to make it for all numbers 1-6) How should I go about doing this?
Im currently trying to use an if-statement to recognize when a specific number is rolled, currently I want the program to recognize the number 6, but im a bit unsure how to display the amount of rolls it took to get that number, in a list, and also keeping the loop going until all rolls have been executed.
private void Btnkast_Click(object sender, EventArgs e)
{
bool throws;
int numberofthrows = 0;
int dice;
Random dicethrow = new Random();
throws = int.TryParse(rtbantal.Text, out numberofthrows);
int[] list = new int[numberofthrows];
for (int i = 0; i <= numberofthrows; i++)
{
dice = dicethrow.Next(1, 7);
if (dice == 6)
{...}
}
}
Also, the only reason I use tryparse is to prevent crashes when having to handle with string-values.
I have written this for you using a C# Console Application, but I'm sure you will be able to edit it to fit your requirements for Windows Forms.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Random rnd = new Random(); // create Random object
Console.WriteLine("Enter a number between 1 and 6: "); // prompt user to enter a number between 1 and 6
int chosenNumberInt;
var chosenNumber = int.TryParse(Console.ReadLine(), out chosenNumberInt); // check to see if user actually entered a number. If so, put that number into the chosenNumberInt variable
Console.WriteLine("How many rolls would you like?"); // prompt user to enter how many rolls they would like to have
int chosenRollsInt;
var chosenRolls = int.TryParse(Console.ReadLine(), out chosenRollsInt);
Console.WriteLine(); // to create space
Console.WriteLine(); // to create space
Console.WriteLine("Chosen Number = " + chosenNumberInt + " --- Chosen Rolls = " + chosenRollsInt); // show user what they entered
Console.WriteLine("------------");
int count = 0;
int numberRolled = 0;
var lstRolls = new List<int>(); // create list object
for(int i = 1; i <= chosenRollsInt; i++)
{
count++;
int dice = rnd.Next(1, 7);
numberRolled = dice;
lstRolls.Add(numberRolled); // add each roll to the list
Console.WriteLine("Roll " + i + " = " + numberRolled); // show each roll
}
var attempts = lstRolls.Count; // how many rolls did you do
var firstIndexOfChosenNumber = lstRolls.FindIndex(x => x == chosenNumberInt) + 1; // have to add 1 because finding the index is 0-based
Console.WriteLine("-------------");
if(firstIndexOfChosenNumber == 0)
Console.WriteLine("The chosen number was " + chosenNumberInt + " and that number was NEVER rolled with " + chosenRollsInt + " rolls.");
else
Console.WriteLine("The chosen number was " + chosenNumberInt + " and the number of rolls it took to hit that number was " + firstIndexOfChosenNumber);
}
}
Something that I didn't add would be the validation to ensure that the user does indeed enter a number between 1 and 6, but you can do that I'm sure.
I have created a DotNetFiddle that proves this code does work and even shows you each roll.
Let me know if this helps or if you need any more assistance.
UPDATE
Based on your comment on my original post, I have edited my code to allow the user to enter the number they want, along with how many rolls. Then, once all of the rolls have been completed, I find the index of the first occurrence of the number they selected in the beginning.
Let me know if this is what you want.
Read the comments I added in the code
private void Btnkast_Click(object sender, EventArgs e)
{
bool throws;
int numberofthrows = 0;
int dice;
Random dicethrow = new Random();
throws = int.TryParse(rtbantal.Text, out numberofthrows);
List<int> list = new List<int>(); //I changed this to a list
for (int i = 0; i < numberofthrows; i++)
{
dice = dicethrow.Next(1, 7);
list.Add(dice); //add every roll to the array to check later the values if you want
if (dice == 6)
{
//Print that you found 6 at the roll number list.Count
Console.WriteLine("Found 6 at roll number: " + list.Count);
break; //probably break the loop so you won't continue doing useless computation
}
}
}
I'm new to programming, and I'm reading a book on C#. This code is not outputting what I was expecting.
Here is the code:
public partial class Form1 : Form
{
static string stars = "****************************************************************";
const int MAXVAL = 52;
const int MAXELEMENTS = 100;
int[] data = new int[MAXELEMENTS];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i;
Random rd = new Random(5);
int j;
string buff;
for (i = 0; i < data.Length; i++)
{
data[i] = rd.Next(MAXVAL);
buff = " ";
for (j = 0; j < data[i]; j++)
{
buff += "*";
lstResult.Items.Add(data[i].ToString() + " " + buff);
}
}
}
}
Here is the output:
Why isn't there random numbers in an random order in the listview?
You are seeding the random instance always with the same number 5. That'll cause repeating numbers. You just have to use the default constructor:
Random rd = new Random();
Have a look at the example on MSDN which shows exactly this behaviour.
By the way, this is also a common pitfall, you should always reuse the same random instance instead of creating always a new one(in a loop) since the seed is created from the current time. If you create randoms very fast you'll get the same seed which causes repeating numbers.
Take out the 5 after the Random(), that is a seed value. Just use the default constructor:
Seed - A integer used to set the starting point for generating a series of random numbers. The seed sets the generator to a random starting point. A unique seed returns a unique random number sequence.
Because you are using 5 as a seed every time, you are getting repeating numbers
Random rd = new Random();
The output is logical if you do your Random.Next call outside the j loop and the Items.Add inside it.
I suggest moving the Items.Add call behind the j loop.
You are using the same seed (5) each time with this line Random rd = new Random(5);
Use the default constructor to generate a random number:
Random rd = new Random();
Declare your random class without seed.
Random rd = new Random();
I have a random number generator and for an assignment we have to take the random numbers and make outputs in a messagebox of the highest and lowest number. I think I need to use if/else somehow but am kind of confused. My code as of now looks like:
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int randomNumber;
for (int i = 0; i < 11; i++)
{
randomNumber = random.Next(1000);
Console.WriteLine(randomNumber);
}
}
}
If you put all the numbers in a collection you can use the LINQ to Objects extension methods Min and Max
Random random = new Random();
List<int> randos = new List<int>();
for (int i = 0; i < 11; i++)
{
randos.Add(random.Next(1000));
}
int min = randos.Min();
int max = randos.Max();
Console.WriteLine("The minimum value is " + min);
Console.WriteLine("The maximum value is " + max);
Because you cannot get the min or max until you've generated the full list, that code needs to be outside of the for loop and you need to put all the random values in a collection so that they persist. I think your problem lies in an attempt to do it all a streaming manner when you must first have a fully formed collection.
Also, if you want to pop up a message box then you should probably create a Windows Forms App rather than a Console Application when creating your project in Visual Studio. If you're working with winforms you can just do MessageBox.Show("My message here") but if you've built a console application you'll have to include a bunch of assemblies to make that work.
If all you care about is just both minimum and maximum of a series of numbers, without storing each one of them, you can just hold the current maximum and minimum on two variables, and update them as the loop progresses. After the final iteration you'll get the max and min of the whole lot:
static void Main(string[] args)
{
Random random = new Random();
int maxNumber;
int minNumber;
maxNumber = minNumber = random.Next(1000); // Assign both variables at once
for (int i = 0; i < 11; i++)
{
int randomNumber = random.Next(1000);
Console.WriteLine(randomNumber);
if (randomNumber > maxNumber) maxNumber = randomNumber;
if (randomNumber < minNumber) minNumber = randomNumber;
}
Console.WriteLine("Maximum: {0}", maxNumber);
Console.WriteLine("Minimum: {0}", minNumber);
Console.ReadKey(true);
}
To show the messagebox in console application you need to set reference to System.Windows.Forms and after the proper using statement then :
Random random = new Random();
List<int> randomNumbers = new List<int>();
for (int i = 0; i < 11; i++)
{
randomNumbers.Add(random.Next(100000));//or set to your desired value
}
//Console.WriteLine("Biggest number is {0} -smallest is {1}", randomNumbers.Max(), randomNumbers.Min());
MessageBox.Show("Biggest number is " + randomNumbers.Max().ToString() + "- smallest is " + randomNumbers.Min().ToString() );
Another method would be to use Linq's Aggregate method:
var random = new Random();
var limits =
Enumerable.Range(0, 11)
.Select(x => random.Next(1000))
.Aggregate(new { min = int.MaxValue, max = int.MinValue },
(a, x) => new
{
min = Math.Min(a.min, x),
max = Math.Max(a.max, x)
});
MessageBox.Show(string.Format("Min: {0}, Max: {1}", limits.min, limits.max));
You just need to gather all random numbers to select the minimum and maximum from them. also you are using Console Application and the MessageBox probably used in Windows Forms but if you want to use it in Console Application you need to import the using System.Windows.Forms; library to use it just by Select:
Project->Add Reference
From the left side Select
FrameWork
and then choose
System.Windows.Forms
and then at the beginning of your code write:
using System.Windows.Forms;
Finally click
OK
and then your code in the Main:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace MyProgram
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
List<int> randomNumbers= new List<int>();
for (int i = 0; i < 11; i++)
{
randomNumbers.Add(random.Next(1000));
}
MessageBox.Show(string.Format("The minimum is: {0}\nThe maximum is: {1}", randomNumbers.Min(), randomNumbers.Max()), "Result");
}
}
}
Generating a random password is easy. but generating a batch is more difficult.
public static string getRandomPassword(int letters, int getallen) {
//int letters = 8;
//int getallen = 5;
char[] letterdeel = new char[letters];
int minGetal = (int)Math.Pow(10, getallen - 1);
int maxGetal = (int)Math.Pow(10, getallen);
string password;
Random r = new Random();
int test = (int)(DateTime.Now.Ticks);
for (int i = 0; i < letters; i++) {
r = new Random((int)(DateTime.Now.Ticks) + i);
bool capital = r.Next(2) == 0 ? true : false;
if (capital) {
letterdeel[i] = (char)r.Next(65, 91);
} else {
letterdeel[i] = (char)r.Next(97, 123);
}
}
password = new string(letterdeel);
password += r.Next(minGetal, maxGetal);
return password;
}
this is my method, the passwords should be in a certain letter-number format.
this works fine, however if i have a for loop pulling 100 passwords from this method, in my array i have 5-8 the same passwords, then again 5-8 the same pass.
i know WHY this is, because of the random function and the clock it depends on, but how do i fix this?
Move Random r to outside the method if you are repeatedly calling it. You are going to be hitting it several times in the same relative timeframe, so you are going to be generating the same seeds. You also want to get rid of the line below. It is unnecessary, and (again), with the nature of DateTime.Now, you would just continue to generate the same sequence of "random" numbers.
r = new Random((int)(DateTime.Now.Ticks) + i);
Define your random number generator as a static outside of the function.
How can I get true randomness in this class without Thread.Sleep(300)?
Use a set rather than whatever collection you store into and don't loop 100 times but until the set has 100 items in it.