Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I wish to know if i can make a simple application in WFA that if you enter your name to generate a lucky number, I have an idea but I really don't know how to generate a number if you insert your name...Is it even posible ?
In C# a string is composed of single characters and those can be converted to int
string s = "Abc";
int i = (int)s[0]; // Get code of first character.
Yet another possibility is to get the hash code of the string:
int hash = s.GetHashCode();
This hash code should look random enough. If not, or if you need to create several random numbers from a name, you could use this hash code as a seed value for the random numbers.
var random = new Random(hash);
int n1 = random.Next();
int n2 = random.Next();
Try this:
var random = new Random();
var number = random.Next(0, 1000);
In this example, number is an int digit between 0 and 1000.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Could someone help me why do i get true only if the numbers are the same? I need to find out if the second input is in the first one. True/false
int[] firstNumber;
firstNumber = new int[10];
firstNumber[0] =
Convert.ToInt32(Console.ReadLine());
int secondNumber =
Convert.ToInt32(Console.ReadLine()) ;
Console.WriteLine(firstNumber.Contains(secondNumber));
Edit. I need to get true for ex.:firstNumber 5214 and secondNumber 4
how can i get true for 5432(firstNum) and 3(secondNum)
Use strings instead of integers, which will use string.Contains instead of Array.Contains:
string firstNumber = Console.ReadLine();
string secondNumber = Console.ReadLine();
Console.WriteLine(firstNumber.Contains(secondNumber));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
how to generate 6 number as => 2 numbers is Const + and 4 numbers is random for Example => 228796 or 225564
First what you intend to be a GUID isn´t a GUID, which is a reversed thing for something like this: "353e1ff6-0493-48f6-953e-15ec5e383034". As of MSDN:
A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required.
Apart from this you can easily create a randomizer that creates numbers between zero and 9999 and use those numbers as your second part:
string constPart = "22";
Random r = new Random();
string myNumber = constPart + r.Next(0, 10000);
You can - even simpler - also use a randomizer for ranges between 220000 and 229999 as follows:
Random r = new Random();
string myNumber = r.Next(220000, 230000).ToString();
Be aware that those numbers aren´t neccessarily unique. That means the more numbers you create, the more does the probability of duplicates increase.
You can simply just use the Random Class.
Make a new Instance of it and use the Next method which has parameters for minimum, maximum.
class Program
{
static void Main(string[] args)
{
int const1 = 1;
int const2 = 2;
Random rng = new Random();
string id = $"{const1}{const2}";
for(int i = 0; i <= 4; i++)
{
id += $"{rng.Next(0, 10)}";
}
Console.WriteLine(id);
Console.ReadKey(true);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
A concrete example.
If i have a range of 1-300, how can a generate 5 unique numbers within that range using GUID "EDAAE218-FBF0-4B66-AEAF-FB036FBF69F4". Applying the same algorithm to the GUID should result in the same 5 numbers being chosen every time.
The input doesn't have to be a GUID, it's just acting as some sort of key.
Some context for the problem i am trying to solve. I have a hard coded List of values that contains roughly 300 or so elements. I am trying to find a way to select 20 elements from this list that always produces the same elements.
My idea was to generate a GUID which could be handed out to multiple users. When those users input the GUID into the app, the same 20 elements would be returned for everyone.
A guid is effectively a 128-bit number. So you can easily do this provided that the number of bits required to represent your numbers are fewer than the number of bits in the guid (128). You don't need to hash the guid or anything like that.
EDIT:
Now that I know what you need (i.e. a unique seed to be derived from a guid, you could do it this way) - but you could equally hand out a 32-bit number and avoid the guid-to-int conversion.
EDIT2: Using GetHashCode as per suggestion from comments above.
EDIT 3: Producing unique numbers.
static void Main(string[] args)
{
var guid = new Guid("bdc39e63-5947-4704-9e12-ec66c8773742");
Console.WriteLine(guid);
var numbers = FindNumbersFromGuid(guid, 16, 8);
Console.WriteLine("Numbers: ");
foreach (var elem in numbers)
{
Console.WriteLine(elem);
}
Console.ReadKey();
}
private static int[] FindNumbersFromGuid(Guid input,
int maxNumber, int numberCount)
{
if (numberCount > maxNumber / 2) throw new ArgumentException("Choosing too many numbers.");
var seed = input.GetHashCode();
var random = new Random(seed);
var chosenSoFar = new HashSet<int>();
return Enumerable.Range(0, numberCount)
.Select(e =>
{
var ret = random.Next(0, maxNumber);
while (chosenSoFar.Contains(ret))
{
ret = random.Next(0, maxNumber);
}
chosenSoFar.Add(ret);
return ret;
}).ToArray();
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to write a program which will calculate the molecular weight of a given molecule based on its chemical formula.
This code can split a molecular formula like "CH3OH" to an array {C H 3 O H} but from here, what would be a good way to use the split text to calculate the molecular weight?
string input = MoleculeTextbox.text;
string pattern = #"([0-9]?\d*|[A-Z][a-z]{0,2}?\d*)";
string[] sunstrings = Regex.Split(input,pattern);
First of all, you'd need to parse the string and turn "H3" into "HHH", etc. That might look something like this:
var x = "CH3OH".replace(/([a-z])([2-9])/gi, function(_,c,n) { return new Array(1+parseInt(n)).join(c); });
First group being the matched character, and second group being the number of repetitions.
You now have a string that looks like "CHHHOH". You can split that line into an array with one character at each index, .split(''), and map each value to its molecular mass. For that you need to define some sort of lookup table. I'm using reduce to take care of the mapping and the addition in one go:
var mass = { C: 12.011, H: 1.008, O: 15.999 };
var weight = x.split('').reduce(function(sum,element) { return sum + mass[element]; }, 0);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a game that involves finding and clicking on a number (0-9) in a grid that randomizes each time you click on the correct one.
I want to get it so that when you click on the correct number, the grid randomizes again.
How would you do this?
Here's what it would kind of look like in the end:
I assume you're rendering an array of integers in order:
for (int i = 0; i < arrayOfNumbers.Length; i++ ) {
// rendering here
render(arrayOfNumbers[i]);
}
If thats the case.. just randomize the array after a successful click.. somewhat like this:
var rnd = new System.Random();
var arrayOfNumbers = Enumerable.Range(1, 9).OrderBy(r => rnd.Next()).ToArray();
Then you can just re-render (or let your game loop continue to render the array). Since the array has changed, your rendering will too.
Every time you detect a click on the correct number (I hope you know how to do this) you simply randomize the array of numbers you're displaying in your grid:
//Fisher-Yates algorithm
Random generator = new System.Random();
int len = array.Length;
while (len > 1)
{
len--;
int k = generator.Next(len + 1);
int temp = array[k];
array[k] = array[len];
array[len] = temp;
}