I am making a program that generates a random number which then is shown to 2 decimal places, I have attempted this but it always comes up with an error.
This is my code:
Random r = new Random();
double ran = r.Next();
int egg;
egg = Console.ReadLine;
Console.WriteLine("The Random number is " + egg);
egg = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(egg);
Console.WriteLine(String.Format("price {0:C}", egg));
Console.ReadLine();
To generate a number with decimal places you should use NextDouble()method.
Random r = new Random();
double egg = r.NextDouble();
Console.WriteLine(String.Format("price {0}", egg.ToString("n2")));
Here is a version which does what your comment states at the beginning.
using System;
class Program
{
static void Main()
{
Random r = new Random();
Console.WriteLine(String.Format("{0:C}", r.Next()));
}
}
Related
I want to generate 10 'random' numbers, but they have to be unique. I have tried something, but is there someone who can help me out with something better?
My code:
List<int> ran = new List<int>();
Random rnd = new Random();
public static int randomValue;
int tempRandom;
public int randomNum()
{
if(ran.Count == 0)
{
ran.Add(0);
ran.Add(1);
ran.Add(2);
ran.Add(3);
ran.Add(4);
ran.Add(5);
ran.Add(6);
ran.Add(7);
}
tempRandom = rnd.Next(0, ran.Count);
randomValue = ran[randomValue];
ran.RemoveAt(tempRandom);
return randomValue;
}
Is this what you're trying to say? If not, please specify how you mean further. This code should give you a number between 1-10 that hasn't been already used. This code will only work 10 times.
Random rnd = new Random();
List<int> usedNumbers = new List<int>();
public int RandomNum(){
int number;
do {
number = rnd.Next(1, 10);
} while(usedNumbers.IndexOf(number) == -1);
usedNumbers.Add(number);
return number;
}
Straight answer to your question (not regarding if you actually want what you are asking for):
Random.Range( int.MinValue, int.MaxValue );
This simply produces a random int in the range of all integers. For 10 numbers, the probability of duplicates is so little that every number will be unique.
Im trying to make random number generator and want it to include max value in array of numbers, what i did was use math.abs and i dont know if i even achieved what i wanted... so here is my code:
using System;
namespace _7
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello this is random number generator!");
Console.WriteLine("Enter min number:");
int pirmas = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter max number:");
int antras = Convert.ToInt32(Console.ReadLine());
antras = Math.Abs(antras + 1);
Random generator = new Random();
Console.WriteLine("Generated number is:");
int end = generator.Next(pirmas, antras);
Console.WriteLine(end);
Console.ReadKey();
}
}
}
The Math.Abs() call is unnecessary and makes the code not work as expected for negative max numbers. Otherwise, what you are doing should work exactly as expected. Just increment antras by one.
antras += 1;
EDIT:
As Haukinger suggested, it would probably be better to not modify the max value (antras) and instead only modify the call to Random.Next().
int end = generator.Next(pirmas, antras + 1);
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();
}
}
}
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");
}
}
}