I been having issues creating a number and letter generator, it should look like this: 9WJLNN8MNDVJCFLQJ4W93YH6ZM:ZWN6QV9ZXG9YCMWAXXWP492DS9
26 letters and numbers randomly colon and same thing after colon, but I keep getting errors but heres my code from what I got so far
Right now I cant even get the numbers and letters together to make it work, im just so confused on what to do. If anyone can help me out that would be amazing. I've been working on this for a few days now.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;
namespace Testing23891721983712983981
{
class Program
{
static void Main(string[] args)
{
{
Random rand = new Random();
int[] numbers = new int[4];
for (int i = 0; i < 4; i++)
{
numbers[i] = rand.Next(1000, 10000);
}
string prefix = string.Join("-", numbers);
for (int i = 0; i < 100; i++)
{
int threeDigits = rand.Next(100, 1000);
RandomGenerator generator = new RandomGenerator();
string str = generator.RandomString(26, false);
Console.WriteLine(threeDigits, str);
Console.ReadKey();
}
}
}
public class RandomGenerator
{
// Generate a random number between two numbers
public int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
// Generate a random string with a given size
public string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
// Generate a random password
public string RandomPassword()
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(4, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
return builder.ToString();
}
}
}
}
use Guid class to generate random numbers with string and then use substring on it.
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)
{
{
Random rand = new Random();
int[] numbers = new int[4];
for (int i = 0; i < 4; i++)
{
numbers[i] = rand.Next(1000, 10000);
}
string prefix = string.Join("-", numbers);
string strguid = "";
for (int i = 0; i < 2; i++)
{
Guid guid = Guid.NewGuid();
if (strguid != "")
{
strguid = strguid + ":" + guid.ToString().Replace("-", "").Substring(0, 26).ToUpper();
}
else
{
strguid = guid.ToString().Replace("-", "").Substring(0, 26).ToUpper();
}
}
Console.WriteLine(strguid);
Console.ReadKey();
}
}
}
}
Related
I am trying to make a Secret Santa code, so that upon running the program, it will take all names from the array and pair them.
I've tried numerous ways of doing this but it just ends up repeating an entry already in the output. fOr example:
Fred and Sarah
Yusef and Kyle
Sarah and Fred
Sarah has come up twice which is not good.
Here's the starting code, of course I first randomise the array, but have no clue what to do after that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp27
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
String[] students = {"Fred","Mary","Yusef","Kyle","Sophie", "Lydia", "Max", "Donald","Yasmin","Archie"};
string[] shuffleStudents = students.OrderBy(x => random.Next()).ToArray();
}
}
}
Any ideas, can anyone help?
I have also tried this which I thought would work but it returns an error of the index being out of bounds of array
static void Main(string[] args)
{
Random random = new Random();
String[] students = { "Fred", "Mary", "Yusef", "Kyle", "Sophie", "Lydia", "Max", "Donald", "Yasmin", "Archie" };
int count = 0;
for (int i = 0; i < 5; i++)
{
string[] shuffleStudents = students.OrderBy(x => random.Next()).ToArray();
Console.Write("{0} and {1}", shuffleStudents[count], shuffleStudents[count+1]);
for (int j = 0; j < 5; j++)
{
count++;
}
}
Console.Read();
}
}
}
You can try to shuffle them, then print the result. Here's the code I did:
using System;
using System.Linq;
namespace _05_01_19_5am
{
class Program
{
public static void Main()
{
Random random = new Random();
String[] students = { "Fred", "Mary", "Yusef", "Kyle", "Sophie", "Lydia", "Max", "Donald", "Yasmin", "Archie" };
var shuffleThem = students.OrderBy(s => Guid.NewGuid()).ToArray();
for (int i = 0; i < 5; i++)
{
Console.WriteLine(shuffleThem[i] + " + " + shuffleThem[i+5]);
}
}
}
}
Fiddled around a bit more and found the solution.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp27
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
string[] students = { "Fred", "Mary", "Yusef", "Kyle", "Sophie", "Lydia", "Max", "Donald", "Yasmin", "Archie"};
string[] shuffleStudents = students.OrderBy(x => random.Next()).ToArray();
Console.WriteLine("Your pairs for Secret Santa has been completed!");
int count = 0;
for (int j = 0; j < 5; j++)
{
Console.Write("{0} and {1} \n", shuffleStudents[count], shuffleStudents[count+1]);
for (int i = 0; i < 2; i++)
{
count++;
}
}
Console.Read();
}
}
}
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();
}
}
I have this method to generate new numeric value but it takes a long time to complete the function.
How to generate random number in fast approch?
public int GeneratenewID(int[] OptionId)
{
Random ran = new Random();
int SearchId = ran.Next(1, OptionId.Length*2);
if (!OptionId.Contains(SearchId))
{
return SearchId;
}
else
{
return GeneratenewID(OptionId);
}
}
this is going to work for sure so try:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int [] OptionId=new int[]
{
0, 1,4,7,3,1,37,9
};
Program p = new Program();
int a= p. GeneratenewID(OptionId);
}
public int GeneratenewID(int[] OptionId)
{
Random ran = new Random(1);
int number = 0;
for (int j = 0; j < OptionId.Length ; j++)
{
number = ran.Next(OptionId.Length);
if (!OptionId.Contains(number))
break;
else
j--;
}
return number;
}
}
}
Could someone explain why do I get this out of bounds?
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testing
{
class Program
{
static void Main(string[] args)
{
var n = int.Parse(Console.ReadLine());
double[] numbers = new double[] { };
for(int i = 0; i < n; i++)
{
var input = double.Parse(Console.ReadLine());
numbers[i] = input;
}
Console.WriteLine(numbers);
}
}
}
You haven't set the array size
double[] numbers = new double[n]
An array is fixed in length upon initialization. Yours has size 0.
double[] numbers = new double[] { }; // { } is the same as 'initialize with 0 elements or no content'
You need to use a List instead of an Array.
List<double> numbers = new List<double>();
for(int i = 0; i < n; i++)
{
var input = double.Parse(Console.ReadLine());
numbers.Add(input);
}