I would like to generate three random ints, which together equal 100. Please help.
int a;
int b;
int c;
int equal = 100;
a = rnd.Next(1, equal);
b = rnd.Next(1, equal) - a;
c = textvar - a - b;
if(equal == a + b + c) {
Console.Write("Work");
}
Change b = rnd.Next(1, equal) - a; to b = rnd.Next(1, equal-a);
Because in the first case b can be less then zero.
Related
I am trying to make a random Trinomial generator and I want the 2 random numbers to follow the trinomial rules (num1+num2=b)(num1*num2=c)
string a = "x²";
int b = new Random().Next(-50, 50);
int c = new Random().Next(-50, 50);
Console.WriteLine(a,b,c);
while (true)
{
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
if ((num1 + num2 == b) && (num1 * num2 == c))
{
Console.WriteLine("Correct.");
break;
}
else
{
Console.WriteLine("Wrong. Try again");
}
}
I expect the numbers to be written down but they aren't. Also, I don't know how to make the random numbers follow these rules. PS - The random numbers are always the same, how do I change that?
Try this:
string a = "x²";
var randomGenerator = new Random();
int b = randomGenerator.Next(-50, 50);
int c = randomGenerator.Next(-50, 50);
Console.WriteLine("{0},{1},{2}", a, b, c);
bool isRunning = true;
while (isRunning)
{
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
if ((num1 + num2 == b) && (num1 * num2 == c))
{
Console.WriteLine("Correct.");
isRunning = false;
}
else
{
Console.WriteLine("Wrong. Try again");
}
}
Console.ReadLine();
Explanation:
First of all the Random problem. Random generates numbers not really in a random way but calculates them. So since it is an algorithm it would work the same every try. To counter that, random seeds itself with the current time which then changes the output of the algorithm. In your case you create 2 random objects, but they will be generated so fast, that both actually seed with the same time, therefore calculating the same "random" numbers. That's why in my solution, we only create one Random object.
Second: If you just want to write one string to the console, jus concat the string and pass it as one parameter.
Here's my attempt at Charles' suggestion:
var rand = new Random();
string a = "x²";
int num1 = rand.Next(-50, 50);
int num2 = rand.Next(-50, 50);
int b = num1 + num2;
int c = num1 * num2;
Console.WriteLine($"{a}, {b}, {c}");
while (true)
{
int guess1 = int.Parse(Console.ReadLine());
int guess2 = int.Parse(Console.ReadLine());
if (guess1 == num1 && guess2 == num2)
{
break;
}
Console.WriteLine("Wrong. Try again");
}
Console.WriteLine("Correct.");
I've simplified the logic at the end a bit, but it should work the same.
I have a problem with calculate of ASCII value with exponent and modulus.
I want calculate ASCII value of "K" with RSA algorithm.
K in ascii value is 75
c = m^e mod n
= 75^41 mod 689
= 316
Then how to make it into source code in C#? I got error "cannot implicity convert type".
this my source code
int n = 689;
int e = 41;
int d = 137;
string value = "K";
for (int i = 0; i < value.Length; i++)
{
int c = (int)i;
c = Math.Pow(i,e);
}
Console.ReadLine();
Since 75^41 will overflow if cast to an int you have to do a little math trick. A*B mod N is equivalent to (A mod N) * (B mod N) mod N, so you just do the multiplication in a loop, taking the remainder each time:
public static int PowModN(int a, int b, int n)
{
a = a % n;
int c = 1;
for(int i=1; i <= b; i++)
c = (c*a % n);
return c;
}
and change your loop to:
for (int i = 0; i < value.Length; i++)
{
int c = (int)i;
c = PowModN(i,e,n);
}
string value = "K";
// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
Once you get the array out put you can set it to a variable and do whatever math you need to do.
The output of Math.Pow is a double, and takes two floats as arguments. At the very least, cast the output of Math.Pow(i,e) to be an int, like so:
c = (int)Math.Pow(i,e)
This is one of the worst things about C#, imo. Not sure why it doesn't innately support integer exponentiation.
What type is i? It might need to be casted to doubles as well.
Let's say I have an 15 as c.
c can be splitted into a = 7 and b = 13.
That's because a | b = c.
I am trying to write a function that will find one possible combination of a and b by only giving c as input.
class Program
{
static void Main(string[] args)
{
int data = 560;
int[] v = FindBitshiftOr(data);
Console.WriteLine("{0} << {1} = {2}", v[0], v[1], v[0] | v[1]);
Console.ReadKey();
}
private static Random r = new Random();
private static int[] FindBitshiftOr(int value)
{
int[] d = new int[2];
int a = r.Next(0, value);
int c = r.Next(0, value);
int b = ~a;
d[0] = a;
d[1] = b | c;
return d;
}
}
This was my attempt but its always returning -1, can someone explain me what's wrong?
Try this code. And operation is used to extract the bits that are same. I don't know C# so I can't type the exact code. The logic is take a random bit mask and AND the bits of c with that bitmask and inverse of that bitmask. The resulting two numbers will be the numbers you need.
int c = 4134;
int a = r.Next(0, value);
int first_num = c & a;
int second_num = c & ~a;
int check_c = first_num | second_num
check_c and c should be equal
You get -1 since you are setting all bits to 1 with OR'ing together number and its negation.
Essential part of your code is:
int a = 42; // you get random number, but it does not matter here.
int b = ~a | c; // "| c" part just possibly add more 1s
result = a | b; // all 1s as it essentially (a | ~a) | c
Proper way of separating bits would be x & ~mask - see links/samples in Most common C# bitwise operations on enums.
How to concatenate integer and string into var?
int a; int x=2; int y=7200;
a=x*y;
var B=a+"D"; // How to concatenate this to turn it 14400D
// I need use this in the code that changes the AxisX.LabelStyle.Interval.
// We can not use string concatenation here.
chart1.ChartAreas[0].AxisX.LabelStyle.Interval=B;
.Interval takes a double, could you not just convert the int to a double instead?
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = Convert.toDouble(a);
Interval method
toDouble
By Looking your code. I think you need this code it seems.?
int a; int x = 2; int y = 7200;
a = x * y;
var B = a.ToString() + "D";
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = B;
OR
int a; int x = 2; int y = 7200;
a = x * y;
String aValue = a.ToString() + "D";
var B = aValue;
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = B;
Exactly If your requirement this, then I would suggest first one.
Instead of int a; write double a; and:
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = a;
For the long time I thought I get it, and I was going to create some puzzles to learn some of my „students“ on the topic of operator precedence in c#.
But it came out that I still don't get it right.
Puzzles:
What’s the output here?
int a = 0;
int x = --a + a++;
Console.WriteLine(x);
Console.WriteLine(a);
Output:
-2
0
All clear here, I expected this
Next, the problem one:
int b = 0;
int y = b-- + b++;
Console.WriteLine(y);
Console.WriteLine(b);
Output:
-1
0
Well, here I also expected y to be -2… Now I’m trying to apply operator precedence rules and order of evaluation, and not sure I explained it to myself.
Read this post again few times today, but still don’t quite get it why is the result here -1?
Can someone help with how is the second result evaluated. why and how is it different than the first one?
b-- is post-decrement. So:
b-- returns zero and subtracts 1 from b, leaving -1 in b.
b++ returns the -1 from the last step and adds 1, leaving 0 in b.
Final result of the addition: -1.
Do what the compiler does: break it down slowly and surely into equivalent programs.
int b = 0;
int y = b-- + b++;
is equivalent to
int b, y;
b = 0;
y = b-- + b++;
is equivalent to
int b, y;
b = 0;
int leftAddend = b--;
int rightAddend = b++;
y = leftAddend + rightAddend;
is equivalent to
int b, y;
b = 0;
int originalb1 = b;
int newb1 = originalb1 - 1;
b = newb1;
int leftAddend = originalb1;
int originalb2 = b;
int newb2 = originalb2 + 1;
b = newb2;
int rightAddend = newb2;
y = leftAddend + rightAddend;
And now annotate each with its value:
int b, y;
b = 0; // b is now 0
int originalb1 = b; // originalb1 is now 0
int newb1 = originalb1 - 1; // newb1 is now -1
b = newb1; // b is now -1
int leftAddend = originalb1; // leftAddend is now 0
int originalb2 = b; // originalb2 is now -1
int newb2 = originalb2 + 1; // newb2 is now 0
b = newb2; // b is now 0
int rightAddend = originalb2;// rightAddend is now -1
y = leftAddend + rightAddend;// y is now -1
This is precisely how the compiler deals with this situation; the compiler is just a bit more clever about optimizing away the temporaries. Analyzing expressions gets easy if you just break it down into simpler steps.
int b = 0;
int y = b-- + b++;
Break it down by step:
y = b--
Here, y is set to b (0), then b is decremented to -1.
+ b++
Here, y (0) is added to b (decremented to -1 in prev step) equaling -1, then b is incremented to zero. Output:
-1 0
Postfix --/++ returns the original value of the variable. So:
With your example b-- + b++:
b-- means decrement b, return the original value. So b = b - 1, b is now -1, and the value of the expression is 0.
b++ means increment b, return the original value. So b = b + 1, b is now 0, and the value of the expression is -1.
Then, 0 + -1 == -1. This is y. b is still 0.
This question has been answered, but I wanted to state the answer another way.
int b = 0;
int y = b-- + b++;
The expression b-- evaluates to 0 (the original value of b) and has the side effect (applied after evaluation) of decrementing b to -1.
The expression b++ evaluates to -1 (because of the previous side effect) and has the side effect (applied after evaluation) of incrementing b to 0.
This leaves the expression 0 + -1 which is -1.