Getting back into the swing of things after focusing on other assignments.
could someone tell me why I get an error for this.
static void Main(string[] args)
{
Console.WriteLine("% unique numbers");
Random rnd = new Random();
for (int count = 0; count <= 5; count++)
{
DiceR(); // ------ Error here -----
}
}
public void DiceR()
{
Random rnd = new Random();
Console.WriteLine(rnd.Next(1, 6));
}
}
make it static
static public void DiceR()
{
Random rnd = new Random();
Console.WriteLine(rnd.Next(1, 6));
}
Related
Goal
A C# program that picks a random number from 1 to 6 and stores it
Question
Which way to store: the entire sequence or an array of 6 elements with increments to the n-1 index for every hit of n?
Code
using System;
namespace DiceProbabilityCalc
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[] instanceCount = new int[1000];
for (int i = 0; i < 999; i++)
{
int num = rnd.Next(1, 7);
}
}
}
}
Thanks to #PeterSmith and #Martheen for solving the question in the comments: The array instanceCount can be used to store the number of instances instead of the entire sequence. This is the resulting code.
using System;
namespace DiceProbabilityCalc
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[] instanceCount = new int[6];
for (int i = 0; i < 999; i++)
{
int num = rnd.Next(1, 7);
instanceCount[num - 1]++;
}
for (int j = 0; j < instanceCount.Length; j++)
{
Console.WriteLine(instanceCount[j]);
}
}
}
}
I need to generate 10 different numbers(integers). My problem is that the first and last number has to be the same. How can I make a code for this logic?
The numbers are later used to populate a polar chart.
Random random = new Random();
int randomNumber = random.Next(5, 16);
int firstRand = 0;
firstRand = randomNumber;
if(indataInt2 == 0)
{
firstRand = randomNumber;
}
else if(indataInt2 >= 360 && firstRand != randomNumber)
{
randomNumber = firstRand;
}
Something like this should do the job
List<int> randomNumber = new List<int>();
Random random = new Random();
for (int i = 0; i < 9; i++)
{
randomNumber.Add(random.Next());
}
randomNumber.Add(randomNumber[0]);
First things first, when using the Random class you can provide a seed in
which will specify how the number is generated. Therefore I provided
a seed for you. This seed is always changing so the random number will
always be different. Remember, Random isn't Random, Random(Seed)
is Random! The list in which you are looking for is named 'Numbers'.
Hopefully this code can help you:
using System.Collections.Generic;
using System;
namespace Degubbing
{
class DebugProgram
{
static void Main(string[] args)
{
List<int> Numbers = new List<int> { };
int Seed = DateTime.Now.Millisecond;
Random Generator = new Random(Seed);
for (int i = 0; i < 10; i++)
{
int RandomNum = Generator.Next(10000000, 20000000);
string Result = RandomNum.ToString();
Result = Result.Remove(Result.Length - 1);
Result = Result + Result[0];
Console.WriteLine(Result);
}
Console.ReadKey();
}
}
}
I've just recently started learning to code C# to hopefully get a job someday. I'm trying to get 3 randomly generated points in a 5x5 grid. For some reason when I try to run it it just auto crashes. For testing purposes I added a Console.WriteLine and Console.ReadKey to try and see the output but it still auto closed immediately. Is there any reason why this shouldn't be working? Thanks for any help :D
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
}
public void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
}
your method is not called. that is why it auto closes.
nothing is done in your program.
class Program
{
static void Main(string[] args)
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
now the output is:
complete code:
class Program
{
static void Main(string[] args)
{
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
This is how i found out that the method was not used:
Before:
After:
As already called out above you are missing the CompB();from the main method .
One more I just looked into the for loop in line 18 it starts from in the i=1 but you are doing
System.Console.WriteLine(AiB[0]);
you should try out something like
System.Console.WriteLine(AiB[1]);
static void Main(string[] args)
{`enter code here`
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
System.Console.WriteLine(AiB[1]);
System.Console.ReadKey();
}
You need to call your method CompB() from the main method. Then you have to write out all the values int the array.
class Program
{
static void Main(string[] args)
{
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
//Write all values:
for (int i = 0; i < 6; i++)
{
System.Console.WriteLine("Value of {0}: {1}", i, AiB[i]);
}
System.Console.ReadKey();
}
}
When I write C# code return the integer array,why it just output 3? it cannot random number.Please help me, thanks!
static void Main(string[] args)
{
Console.WriteLine(3);
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
}
return array;
}
static void Main(string[] args)
{
var output = RandomArray(10);
for (int i = 0; i < output.Length; i++)
{
Console.WriteLine(output[i]);
}
Console.WriteLine();
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
}
return array;
}
In output variable, you will get array. You can use it further.
You can do this.
The problem is you were just writing 3 to the console
private static Random rand = new Random();
public static void Main(string[] args)
{
foreach(var item in RandomArray(3))
Console.WriteLine(item);
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
for (int i = 0; i < items; i++)
array[i] = rand.Next(100, 200);
return array;
}
Demo Here
Random Class
Console.WriteLine Method
Main is the entry point in a C# application. Main is called by the common language runtime (CLR) at the start of your program's execution. In other words, Main is called automatically. RandomArray is also a method. However, it is not an Entry point and therefore it will not be called automatically. You have to call RandomArray from main in order for the method to execute.
Right now your program is effectively just:
static void Main(string[] args)
{
Console.WriteLine(3);
}
The rest of the code is just sitting around doing nothing.
In order to fix this problem you need to call RandomArray from Main
static void Main(string[] args)
{
Console.WriteLine(3);
RandomArray(5);
}
And if you also want to output more data to the console then you can do something like:
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
Console.WriteLine($"{array[i]} inserted into array");
}
return array;
}
Presumably you also want to do something with the return value of RandomArray. Modify Main to assign the return value to a variable. Then you can do something with the result:
static void Main(string[] args)
{
Console.WriteLine(3);
int[] result = RandomArray(5);
foreach (var element in result)
{
Console.WriteLine(element);
}
}
i am building a small app that act like a digital camera, and on my takephoto function i want to insert a random number to my array(i succeed doing that), my problem is that when i take multiple pictures the value always go into the first postion. my code:
public override void TakePhoto()
{
Random rnd = new Random();
int photo = rnd.Next(1, 10);
for (int i = 0; i < 1; i++)
{
MemoryCard.buffer[i] = photo;
}
}
class Program
{
static void Main(string[] args)
{
DigitalCamera digitalCamera =
new DigitalCamera("kodak", 43, newMemoryCard, 3);
digitalCamera.TakePhoto();
digitalCamera.TakePhoto();
digitalCamera.TakePhoto();
}
}
how do i jump to the next position after each photo?
You esplicitly say to put next value in the first position.
Look on your code:
for (int i = 0; i < 1; i++)
{
MemoryCard.buffer[i] = photo;
}
i is always 0
To resolve this, just save an i value into some global variable, or next index accept like a parameter in the TakePhoto(...) function.
Example:
int curindex = 0; //GLOBAL VARIABLE
public override void TakePhoto()
{
Random rnd = new Random();
int photo = rnd.Next(1, 10);
if(curindex < MemoryCard.buffer.Length) //IF CURINDEX LESS THE TOTAL ARRAY LENGTH
{
MemoryCard.buffer[curindex] = photo; //ASSIGN PHOTO IN CURRENTINDEX
curindex ++; //INCEREMENT CURRENTINDEX
}
}
You have a bug in your for-loop, i assume you want to use the Length of the buffer variable instead
You are calling TakePhoto too fast, therefore you create the Random always with the same seed, hence always the same "random" number is generated.
Instead pass the Random instance as parameter to the method or use a field variable.
public override void TakePhoto(Random rnd)
{
int photo = rnd.Next(1, 10);
for (int i = 0; i < MemoryCard.buffer.Length; i++)
{
MemoryCard.buffer[i] = photo;
}
}
Now always use the same instance:
Random r = new Random();
DigitalCamera digitalCamera = new DigitalCamera("kodak", 43, newMemoryCard, 3);
digitalCamera.TakePhoto(r);
digitalCamera.TakePhoto(r);
digitalCamera.TakePhoto(r);
I think following modification to your code will resolve your issue:
class DigitalCamera
{
static int currentPhotoNumber = 0;
private Random rnd = new Random();
public override void TakePhoto()
{
int photo = rnd.Next(1, 10);
MemoryCard.buffer[currentPhotoNumber++] = photo;
}
}
class Program
{
static void Main(string[] args)
{
DigitalCamera digitalCamera = new DigitalCamera("kodak", 43, newMemoryCard, 3);
digitalCamera.TakePhoto();
digitalCamera.TakePhoto();
digitalCamera.TakePhoto();
}
}
There is a logic error in your code here:
for (int i = 0; i < 1; i++)
{
MemoryCard.buffer[i] = photo;
}
If you look at your for-loop variables, the loop is running from i=0, to i<1. Which basically means that the loop is only running once (for i=0).
You need to increase i each time, perhaps
MemoryCard.buffer[MemoryCard.numberofphotos] = photo;
MemoryCard.numberofphotos++;