How do I convert a value from Random to a string? - c#

I'm new to c# and I would like to use a value that a random method would give me from an actual array list. But I can't seem to convert it or use a console.readline() on it. Here goes my code.
static void Main(string[] args)
{
string[] mots = new string[] { "pomme", "animal", "pont", "ensemble", "visuel", "acronyme", "cellule", "article", "syllable", "programme" };
Random mot = new Random(mot.GetHashCode());
mot = Console.ReadLine();
Console.WriteLine(mots[mot.Next(9)]);
string mot = "pomme";
char[] charArray = mot.ToCharArray();
Array.IndexOf(charArray);
}
How do I convert the output into a string in order to use it afterwards? Thank you.

To get and output to the console a random element from the mots array:
var mots = new [] { "pomme", "animal", "pont", "ensemble", "visuel", "acronyme", "cellule", "article", "syllable", "programme" };
var rand = new Random();
var index = rand.Next(0, mots.Length); // returns a random int between 0 and mots.Length - 1 (see below)
var mot = mots[index];
var motString = mot.ToString(); // get the string value
Console.WriteLine("Random element: {0}. String value: {1}.", mot, motString);
From the Microsoft Random.Next documentation
The Next(Int32, Int32) overload returns random integers that range from minValue to maxValue – 1. However, if maxValue equals minValue, the method returns minValue.

Try it with the index. Create a random value with max value of the array length - 1. Then return the string at this random index.
string[] mots = new string[] { "pomme", "animal", "pont", "ensemble", "visuel", "acronyme", "cellule", "article", "syllable", "programme" };
Random Rnd = new Random();
string RandomString()
{
return mots[Rnd.Next(0, mots.Length - 1)];
}
Hope this helps. Feel free to ask.

Related

generate 13 digit unique random number in c#

Modify the follwing code to generate 13 digit unique random number in c#
public static string GenerateUniqueRandomNumbers()
{
Random generator = new Random();
String r = generator.Next(0, 1000000).ToString("D6");
if (r.Distinct().Count() == 1)
{
r = GenerateUniqueRandomNumbers();
}
return r;
}
Very malformed question
IF your problem is that maxValue cannot have 13 digit, a quick workaround could be concatenate 2 strings
String r = generator.Next(0, 1000000).ToString("D6");
r += generator.Next(0, 10000000).ToString("D7");
For 13 digits we need long variable but Random.Next method does not support long dataType it only support Integer data type. Hence , we have to do something tricky.
Check the below code to generate generate 13 digit number.
Random generator = new Random();
String r = generator.Next(0, 999999).ToString("D13");
Note: I have used ToString("D13") to get the 13 digits value.
public static string GenerateUniqueRandomNumbers()
{
Random generator = new Random();
String r = generator.Next(0, 1000000).ToString("D6");
r += generator.Next(0, 10000000).ToString("D7");
if (r.Distinct().Count() == 1)
{
r = GenerateUniqueRandomNumbers();
}
return r;
}
Random generator = new Random();
string s = "380003562";
s += generator.Next(0, 0000000).ToString("D"+(13-s.Length).ToString());
With this code, if you are using an existing number, you can quickly bring it to 13 digits or any digit number you want.

Adding random values from array with some probability

Let's say I have a code for a windows form, that generates greetings when you press a button.
string[] Greetings = new string[] { "Hi", "Hello", "Howdy!", "Hey" };
string[] Smilies = new string[] {";)", ":)", "=)", ":-)" };
Random rand = new Random();
string Greet = Greetings[rand.Next(0, Greetings.Length)];
string Smile = Smilies[rand.Next(0, Smilies.Length)];
TextBox.Text = Greet + " " + Smile;
Clipboard.SetText(TextBox.Text);
What if I want to add smilies with a probability of X%. So that they do not appear all the time, but with a chance I set in the code? What is a good way to do it?
I thought of something like this --
public void chance (string source, int probability)
{
Random chanceStorage = new Random();
if (probability >= chanceStorage.Next(0, 100))
TextBox.Text = source;
}
And then
TextBox.Text = Greet;
chance("_" + Smile, X);
Is that optimal?
50% chance to smile:
string[] Greetings = new string[] { "Hi", "Hello", "Howdy!", "Hey" };
string[] Smilies = new string[] {";)", ":)", "=)", ":-)" };
Random rand = new Random();
string Greet = Greetings[rand.Next(0, Greetings.Length)];
string Smile = rand.NextDouble() > 0.5 ? " "+Smilies[rand.Next(0, Smilies.Length)] : string.Empty;
TextBox.Text = Greet + Smile;
I'd simply generate a random double between 0, 1 (inclusive of 0 only) with random.NextDouble(). Then you can just check that a probability (as a value between 0 and 1) is less than the generated number.
For example
double chance = 0.35; //35%
bool createSmiley = rand.NextDouble() < chance;
This is not necessarily exactly precise (because of the finite precision of floating point numbers) but will get you very close. And allows for more precision than just using ints as your random numbers.
A demo of the random generation of smileys.
On another note, I'd refactor your code a bit to better isolate the logic. I'd personally just try to manipulate a single string, and set the .Text property once after all the string processing is done.
I would divide the length of your Smilies array by your probability to give a range of random numbers to generate.
4 smilies / 25% probablity = 16. So your random generator would generate a random number from 0 - 15 giving a 25% chance of using a smiley. If the generated number is outsides the bounds of the array, don't use a smiley.
using System;
public class Program
{
public static void Main()
{
Random rand = new Random();
double percentage = rand.NextDouble();
string[] greetings = new[] { "Hi", "Hello", "Howdy!", "Hey" };
string[] smilies = new[] { ";)", ":)", "=)", ":-)" };
// Get a greeting
string greet = greetings[rand.Next(0, greetings.Length)];
// Generate the smiley index using the probablity percentage
int randomIndex = rand.Next(0, (int)(smilies.Length / percentage));
// Get a smiley if the generated index is within the bound of the smilies array
string smile = randomIndex < smilies.Length ? smilies[randomIndex] : String.Empty;
Console.WriteLine("{0} {1}", greet, smile);
}
}
See working example here... https://dotnetfiddle.net/lmN5aP

Generate random 6 digit number

I've been searching for a couple of hours and I just can't seem to find a answer to this question. I want to generate a random number with 6 digits. Some of you might tell me to use this code:
Random generator = new Random();
int r = generator.Next(100000, 1000000);
But that limits my 6 digits to all be above 100 000 in value. I want to be able to generate a int with 000 001, 000 002 etc. I later want to convert this integer to a string.
If you want a string to lead with zeroes, try this. You cannot get an int like 001.
Random generator = new Random();
String r = generator.Next(0, 1000000).ToString("D6");
You want to have a string:
Random r = new Random();
var x = r.Next(0, 1000000);
string s = x.ToString("000000");
For example,
x = "2124"
s = "002124"
As stated in a comment, a "six digit number" is a string. Here's how you generate a number from 0-999999, then format it like "000482":
Random r = new Random();
int randNum = r.Next(1000000);
string sixDigitNumber = randNum.ToString("D6");
private static string _numbers = "0123456789";
Random random = new Random();
private void DoWork()
{
StringBuilder builder = new StringBuilder(6);
string numberAsString = "";
int numberAsNumber = 0;
for (var i = 0; i < 6; i++)
{
builder.Append(_numbers[random.Next(0, _numbers.Length)]);
}
numberAsString = builder.ToString();
numberAsNumber = int.Parse(numberAsString);
}
I agree with the comment above that 000 001 can't be an integer, but can be a string with:
Random generator = new Random();
int r = generator.Next(1, 1000000);
string s = r.ToString().PadLeft(6, '0');
string s = generator.Next(0, 1000000).ToString("D6");
or
string s = generator.Next(0, 1000000).ToString("000000");
This is a better solution, because it gives you more random numbers and is not based on the system clock.
Cryptographic random number generators create cryptographically strong random values.
System.Security.Cryptography.RandomNumberGenerator.GetInt32(0, 1000000);
To ensure that the resulting value has six digits, you can convert to string and append zeros at the start. e.g GeneratedValue.ToString().Padleft(6,'0') using Matt's method above.
See official documentation for additional details.
solutions that use
Random generator = new Random();
int r = generator.Next(1, 1000000);
dont seem to work always.
I am calling the following method in a class constructor and it gives same number after the first time.
internal string GenerateID()
{
Random r = new Random();
int randNum = r.Next(1000000);
string sixDigitNumber = randNum.ToString("D6");
return sixDigitNumber;
}
Attached is image of the debug statement in .net unitTest for the class.
My project is .net 4.8.
I have moved to using System.Security.Cryptography.RandomNumberGenerator as suggested above.

Replace word/value in text with dynamic number

For ex. I have string
string text = #"Today is {Rand_num 15-22} day. {Rand_num 11-55} number of our trip.";
I need to replace every Rand_num constraction with rand number (between stated numbers 15-22 or 11-55)
Tried smth but don't know what to do next
string text = #"Today is {Rand_num 15-22} day. {Rand_num 11-55} number of our trip.";
if (text.Contains("Rand_num"))
{
string groups1 = Regex.Match(text, #"{Rand_num (.+?)-(.+?)}").Groups[1].Value;
string groups2 = Regex.Match(text, #"{Rand_num (.+?)-(.+?)}").Groups[2].Value;
}
How about:
Random rand = new Random();
text = Regex.Replace(text, #"{Rand_num (.+?)-(.+?)}", match => {
int from = int.Parse(match.Groups[1].Value),
to = int.Parse(match.Groups[2].Value);
// note end is exclusive
return rand.Next(from, to + 1).ToString();
});
Obviously output will vary, but I get:
Today is 21 day. 25 number of our trip.
You can use Random Method, Check the Code Snippet if it suites you.
Random random=new Random();
string text=String.Format("Today is {0} day. {1} number of our trip.", random(15-22), random(11-15));

How to generate string based on Pattern

I have a case where I have to generate transaction number based on specific pattern
The pattern is following:
MA 0000000/dd/mm/YYYY/00000
where first zeros are random numbers then current date and last zeros should be incremental
(00001... 00010... 00100... 00578)
Could you please provide correct way to implement this case.
public static class Generator
{
static int current = 0;
static Random rand = new Random();
public static string NextId()
{
return string.Format("MA {0:0000000}/{1}/{2:00000}",
rand.Next() % 100000,
DateTime.Now.ToString("dd/MM/yyyy"),
current++ );
}
}
public string NextId(int lastCount)
{
var rand = new Random();
return string.Format("MA{0:0000000}/{1}/{2:00000}",
rand.Next(9999999),
DateTime.Today.ToString("dd/MM/yyyy"),
lastCount + 1);
}
Random rand = new Random();
int randomNumber = rand.Next(100000000);
int counter = 1;
string str = "MA" + randomNumber.ToString() + DateTime.Now.ToString("/dd/MM/yyyy/") + counter.ToString("X4");
Console.WriteLine(str);
Probably you are concerned about two things:
DateTime format: you may use: DateTime.Now.ToString("/dd/MM/yyyy/")
and
Padding leading zeros to a number, you may use:
counter.ToString("X4")
Also see: How to: Pad a Number with Leading Zeros
Random rand = new Random();
string random = rand.Next(10000000).ToString("D7");
string date = DateTime.Today.ToString("dd/MM/yyyy");
string increment = inc.ToString("D5");
String.Format("MA {0}/{1}/{2}", random, date, increment);
Where inc is the incremental number. You should know this, or otherwise find out what the next number should be.
for(int i=0;i<n;i++)
{
Console.WriteLine("MA "+(new Random()).Next(0,100).ToString("D7")+DateTime.Now.ToString("dd/MM/yyyy")+ i.ToString("D5"));
}
Where Next() generates random number between 0 and 100
i.ToString("D5") will give you number in 00001.. format

Categories

Resources