I was trying to make a lucky dip program where 6 random numbers,1-59, are chosen then printed out in an array. I managed to get this to work, however you needed to use an IndexOf method so that no same number was printed twice by checking if the new number is already in the array.
using System;
namespace LuckyDip
{
class Program
{
static void Main(string[] args)
{
int[] luckyNumbers = new int[6];
Random random = new Random();
for (int x = 0; x<6; x++)
{
num[x] = random.Next(1,59);
Console.WriteLine(num[x]);
}
Console.ReadLine();
}
}
}
It prints out numbers, but sometimes they are the same.
You state that you want to use IndexOf, but that method is used for strings (see docs). Your example has an int array so the solution below uses Contains. This solution adds a check within your loop and generates a new number if this number already exists within your array. If you really need to use IndexOf, create a string array and convert the numbers using String.valueOf(randomNumber)
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] luckyNumbers = new int[6];
Random random = new Random();
for (int x = 0; x<6; x++)
{
int randomNumber = random.Next(1,59);
while (luckyNumbers.Contains(randomNumber))
{
randomNumber = random.Next(1,59);
}
luckyNumbers[x] = randomNumber;
Console.WriteLine(luckyNumbers[x]);
}
}
}
Another possible solution would be:
using System;
using System.Collections;
using System.Linq;
public class Program
{
public static void Main()
{
int arraySize = 6;
int[] luckyNumbers = Enumerable.Range(1,59).OrderBy(g => Guid.NewGuid()).Take(arraySize).ToArray();
for (int x = 0; x < luckyNumbers.Length; x++)
{
Console.WriteLine(luckyNumbers[x]);
}
}
}
Related
I attempting to create a program that uses multiple methods that would print out base numbers, exponents, and their resulting solutions. I am trying to get it to run and it's nearly completed, but I am encountering a couple issues. The code itself seems to run, but doesn't appear to print out on Visual Studio. I did run it on an online compiler and got this as an output:
It seems I am missing something in my code, but I am unclear as to what I may be missing. This is the code I have created for the project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project
{
class Program
{
static void Main(string[] args)
{
//Our initialized variables.
int intMinBase = 1;
int intMaxBase = 100;
int intMinExpo = 1;
int intMaxExpo = 10;
//Our arrays for the project, all at a length of 5.
long[] baseNumbers = new long[5];
long[] exponents = new long[5];
long[] results = new long[5];
//Randomize the baseNumbers and exponents!
Random randInt = new Random();
for (long i = 0; i < 5; i++)
{
baseNumbers[i] = randInt.Next(intMinBase, intMaxBase);
exponents[i] = randInt.Next(intMinExpo, intMaxExpo);
}
PrintArrays(baseNumbers, exponents, results);
}
//This is potentially experimental code for the Power Method.
public static int Power(int baseNum, int exponent)
{
int answer;
if (exponent == 1)
{
answer = 1;
}
else
{
answer = baseNum * Power(baseNum, exponent - 1);
}
return answer;
}
//The new method to be printed. Is this the correct manner to display this?
public static void PrintArrays(long[] baseNum, long[] exponent, long[] result)
{
Console.WriteLine($"Base\tExponent\tResult");
for (int print = 0; print < result.GetUpperBound(0); print++)
{
Console.WriteLine(baseNum[print]+"\t"+exponent[print]+"\t"+result[print]);
}
}
}
}
My question is mainly am I missing something and why isn't it appearing to print in Visual Studio yet it's appearing on an online compiler? I suspect the answer to the first part of the question has to do with the methods I used, but I am unsure.
First error: Nowhere is the method Power called and nowhere is the array results filled.
Solution example:
for (long i = 0; i < 5; i++)
{
baseNumbers[i] = randInt.Next(intMinBase, intMaxBase);
exponents[i] = randInt.Next(intMinExpo, intMaxExpo);
results[i] = Power(baseNumbers[i], exponents[i]);
}
This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 4 years ago.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DieRoller
{
public class Program
{
public static void Main()
{
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(RollDie());
}
Console.ReadLine();
}
public static int RollDie()
{
Random roll = new Random();
int test = roll.Next(1, 6 + 1);
return test;
}
}
}
When I execute this code I get the number 4 multiple times or the number 2 multiple times...etc.
Isn't it supposed to execute the RollDie function for each iteration of the loop? and isn't that supposed to yield a different value each time? pls halp!
EDIT: The thing is guys, I need to generate the randomness only inside the RollDie method, and I can't have any arguments for the RollDie method (Basically I have to generate the randomness only using the random method inside the RollDie method), other questions don't address that.
See comments for explanation of why it doesn't work. Here a possible way to make it work:
public static void Main()
{
Random roll = new Random();
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(RollDie(roll));
}
Console.ReadLine();
}
public static int RollDie(Random roll)
{
int test = roll.Next(1, 6 + 1);
return test;
}
Or, for simplicity, just:
public static void Main()
{
Random roll = new Random();
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(roll.Next(1, 6 + 1));
}
Console.ReadLine();
}
The code is to generate random numbers in 100 files numbered from 0..99.
What I couldn't get was why this code ended up creating a file called 100.txt and I even got an exception saying that 100.txt was being written by another process.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RandomNumbersFileGenerator
{
class Program
{
static Random Random = new Random();
static void Main(string[] args)
{
List<Task> tasks = new List<Task>();
for(int fileNumber = 0; fileNumber < 100; ++fileNumber)
{
tasks.Add(Task.Run(()=>GenerateFileWithRandomNumbers(Path.Combine($"c:\\FilesWithRandomNumbers\\{fileNumber}.txt"), 10000000)));
}
Task.WaitAll(tasks.ToArray());
}
static void GenerateFileWithRandomNumbers(string path, int numberOfNumbers)
{
List<string> listOfNumbers = new List<string>();
for(;numberOfNumbers > 0; --numberOfNumbers)
{
listOfNumbers.Add(Random.Next().ToString());
}
File.WriteAllLines(path, listOfNumbers);
}
}
}
It is related to closures and captured variables. Change
for(int fileNumber = 0; fileNumber < 100; ++fileNumber)
{
tasks.Add(Task.Run(()=>GenerateFileWithRandomNumbers(Path.Combine($"c:\\FilesWithRandomNumbers\\{fileNumber}.txt"), 10000000)));
}
To
for(int fileNumber = 0; fileNumber < 100; ++fileNumber)
{
int tmp = fileNumber;
tasks.Add(Task.Run(()=>GenerateFileWithRandomNumbers(Path.Combine($"c:\\FilesWithRandomNumbers\\{tmp}.txt"), 10000000)));
}
See also http://csharpindepth.com/articles/chapter5/closures.aspx
In my project that i'm working on I have to match 5 numbers for yahtzee. So all these numbers have to be the same. Now I have thought about how to do this but i'm not sure about what the best and easiest way is. Sure I can write it all out but there has to be a shorter way.
I haven't written the code for the part that checks if yahtzee has been thrown. This is because I only can come up with one way and that is to write it all out.
Here's my code so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Opdr3
{
class Program
{
struct YahtzeeGame
{
public int[] dobbelstenen;
public Random rnd;
public void Gooi()
{
for (int i = 0; i < 5; i++)
{
dobbelstenen[i] = Int32.Parse(rnd + "");
}
}
public bool Yahtzee()
{
Here it has to check if all dobbelstenen[int]
are the same
}
}
static void Main(string[] args)
{
// maak YahtzeeGame (struct) aan
YahtzeeGame yahtzeeGame;
// initialiseer struct-members
yahtzeeGame.rnd = new Random();
yahtzeeGame.dobbelstenen = new int[5];
// probeer yahtzee te gooien
int aantalPogingen = 0;
do
{
// gooi alle dobbelstenen
yahtzeeGame.Gooi();
aantalPogingen++;
} while (!yahtzeeGame.Yahtzee());
// vermeld aantal pogingen voor yahtzee
Console.WriteLine("Aantal pogingen nodig: {0}", aantalPogingen);
// wacht op gebruiker
Console.ReadKey();
}
}
}
You'll need a little loop:
public bool Yahtzee()
{
// check if all dobbelstenen[int] are the same
for(int i = 1; i < 5; i++) // start with second dobbelstenen
{
if(dobbelstenen[i] != dobbelstenen[0]) return false;
}
return true;
}
It simply compares second, third, ... against the first.
I'm having trouble with this method return of the largest integer, compiler says no errors but it won't let me run it
I have no clear idea what you are asking for.. I think at one time you had code but it is now gone?
Anyway, here is a console example for making an array and displaying its max value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//Declare i
int i;
Console.WriteLine("Please enter 5 random numbers");
//Make some array
string[] numbers = new string[5];
for (i = 0; i < 5; i++)
{
Console.Write("\nEnter your number:\t");
//Storing value in an array
numbers[i] = Console.ReadLine();
}
//ta da your array is now completed.. lets see what is the largest..
var converted = numbers.Select(int.Parse);
int largest = converted.Max();
//ta da
Console.WriteLine("The largest number is..." + (largest));
}
}
}