I was looking into random number generators and found pseudo code for one:
function Noise1(integer x)
x = (x<<13) ^ x;
return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0);
end function
I would like to convert this into C# but I get all kinds of error like invalid expressions and ")" expected. This is what I have so far how can I convert it?
double Noise(int x) {
x = (x<<13) ^ x;
return ( 1.0 - ((x * (x * x * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0);
}
Thanks.
I don't know what language you've started with, but in C# hex constants should look differently: change 7fffffff to 0x7fffffff.
You can use the .Net Framework random object
Random rng = new Random();
return rng.Next(10)
But I strongly recommend you to read this article from Jon Skeet about random generators
http://csharpindepth.com/Articles/Chapter12/Random.aspx
EDIT: tested and reported a non-null sequence
Convert your hexadecimal constants to use "0x" prefix
Convert int <-> double carefully
Split the expression to make it a little bit more readable
Here's the code and unit test (strange results though):
using System;
namespace Test
{
public class Test
{
public static Int64 Noise(Int64 x) {
Int64 y = (Int64) ( (x << 13) ^ x);
Console.WriteLine(y.ToString());
Int64 t = (y * (y * y * 15731 + 789221) + 1376312589);
Console.WriteLine(t.ToString());
Int64 c = t < 0 ? -t : t; //( ((Int32)t) & 0x7fffffff);
Console.WriteLine("c = " + c.ToString());
double b = ((double)c) / 1073741824.0;
Console.WriteLine("b = " + b.ToString());
double t2 = ( 1.0 - b);
return (Int64)t2;
}
static void Main()
{
Int64 Seed = 1234;
for(int i = 0 ; i < 3 ; i++)
{
Seed = Noise(Seed);
Console.WriteLine(Seed.ToString());
}
}
}
}
Related
I have a lot of dislikes on this post, I'm not sure why, but for letting you guys help me out with this question, I will give you this script as a gift. This script converts experience to level and from level to experience points given an exponential expression. those constants ensure that level 100 will equal 10 million experience. In Runescape, their level 99 equals 13,032,xxx which is a strange number.
using System.IO;
using System;
class Program
{
const float salt = 2.82842712474619f;
const float factor = 0.64977928f;
const int lvl_100_XP = 10000000;
static void Main()
{
int xp = 9717096;// lvl 99
int lvl = ExperienceToLevel(xp);
Console.WriteLine("LVL: " + lvl.ToString()+ " XP: " + LevelToExperience(lvl).ToString());
}
static public int ExperienceToLevel(int xp){
int lvl = 0;
if (xp == lvl_100_XP){//9999987 is lvl 100 due to roundoff issues so it is fixed to 10mill
lvl = 100;
}
else{
lvl = (int)((1f / salt) * (float)Math.Pow((float)xp, (1f - factor)));
if (lvl == 0){
//lvl = 1;
}
}
if (lvl == 100 && xp < lvl_100_XP){
lvl = 99;
}
return lvl;
}
static public int LevelToExperience(int lvl){
int xp = 0;
if (lvl == 100){//9999987 is lvl 100 due to roundoff issues so it is fixed to 10mill
xp = lvl_100_XP;
}
else{
xp = (int)Math.Exp((float)Math.Log(salt * (float)lvl) / (1f - factor))+1;
if (xp <= 1){
xp= 0;
}
if (lvl == 100){
xp = lvl_100_XP;
}
}
return xp;
}
}
Let's work it out.
Let x be the experience, a and c are constants. L is the level. We notate exponentiation as ^; note that C# does not have such an operator. ^ in C# is XOR.
You have
b = x / a
d = x ^ c
L = b / d
so that's
L = x / (a * x ^ c)
which is
L = (1 / a) * (x / x ^ c)
which is
L = (1 / a) * x ^ (1 - c)
You wish to solve for x. So multiply both sides by 'a':
a * L = x ^ (1 - c)
Take the ln of both sides. (Or whatever logarithm you like best.)
ln (a * L) = (1 - c) ln (x)
Divide both sides by 1 - c
ln (a * L) / (1 - c) = ln x
And eliminate the ln; remember that exp is the inverse of ln. If you used some other logarithm, then use some other exponent.
exp (ln (a * L) / (1 - c)) = x
And we're done.
First of all, exp to level is a many to one relationship. The reverse is a one to many, so what exp are you aiming to return in lvl2exp? The minimum for that level? The maximum? A mean value?
I think your better off just computing once a table with experience brackets for each level (even cache it to a file if its not going to change) and simply doing a binary search to find the corresponding bracket for any given level.
I have this function wrote in C# to calc the sin(x). But when I try with x = 3.14, the printed result of sin X is NaN (not a number),
but when debugging, its is very near to 0.001592653
The value is not too big, neither too small. So how could the NaN appear here?
static double pow(double x, int mu)
{
if (mu == 0)
return 1;
if (mu == 1)
return x;
return x * pow(x, mu - 1);
}
static double fact(int n)
{
if (n == 1 || n == 0)
return 1;
return n * fact(n - 1);
}
static double sin(double x)
{
var s = x;
for (int i = 1; i < 1000; i++)
{
s += pow(-1, i) * pow(x, 2 * i + 1) / fact(2 * i + 1);
}
return s;
}
public static void Main(String[] param)
{
try
{
while (true)
{
Console.WriteLine("Enter x value: ");
double x = double.Parse(Console.ReadLine());
var sinX = sin(x);
Console.WriteLine("Sin of {0} is {1}: " , x , sinX);
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
It fails because both pow(x, 2 * i + 1) and fact(2 * i + 1) eventually return Infinity.
In my case, it's when x = 4, i = 256.
Note that pow(x, 2 * i + 1) = 4 ^ (2 * 257) = 2.8763090157797054523668883052624395737887631663 × 10^309 - a stupidly large number which is just over the max value of a double, which is approximately 1.79769313486232 x 10 ^ 308.
You might be interested in just using Math.Sin(x)
Also note that fact(2 * i + 1) = 513! =an even more ridiculously large number which is more than 10^1000 times larger than the estimated number of atoms in the observable universe.
When x == 3.14 and i == 314 then you get Infinity:
?pow(-1, 314)
1.0
?pow(x, 2 * 314 + 1)
Infinity
? fact(2 * 314 + 1)
Infinity
The problem here is an understanding of floating point representation of 'real' numbers.
Double numbers while allowing a large range of values only has a precision of 15 to 17 decimal digits.
In this example we are calculating a value between -1 and 1.
We calculate the value of the sin function by using the series expansion of it which is basically a the sum of terms. In that expansion the terms become smaller and smaller as we go along.
When the terms have reached a value less than 1e-17 adding them to what is already there will not make any difference. This is so because we only have 52 bit of precision which are used up by the time we get to a term of less than 1e-17.
So instead of doing a constant 1000 loops you should do something like this:
static double sin(double x)
{
var s = x;
for (int i = 1; i < 1000; i++)
{
var term = pow(x, 2 * i + 1) / fact(2 * i + 1);
if (term < 1e-17)
break;
s += pow(-1, i) * term;
}
return s;
}
I have next code
int a,b,c;
b=1;
c=36;
a=b%c;
What does "%" operator mean?
It is the modulo (or modulus) operator:
The modulus operator (%) computes the remainder after dividing its first operand by its second.
For example:
class Program
{
static void Main()
{
Console.WriteLine(5 % 2); // int
Console.WriteLine(-5 % 2); // int
Console.WriteLine(5.0 % 2.2); // double
Console.WriteLine(5.0m % 2.2m); // decimal
Console.WriteLine(-5.2 % 2.0); // double
}
}
Sample output:
1
-1
0.6
0.6
-1.2
Note that the result of the % operator is equal to x – (x / y) * y and that if y is zero, a DivideByZeroException is thrown.
If x and y are non-integer values x % y is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / y (more details in the C# 4.0 Specification in section 7.8.3 Remainder operator).
For further details and examples you might want to have a look at the corresponding Wikipedia article:
Modulo operation (on Wikipedia)
% is the remainder operator in many C-inspired languages.
3 % 2 == 1
789 % 10 = 9
It's a bit tricky with negative numbers. In e.g. Java and C#, the result has the same sign as the dividend:
-1 % 2 == -1
In e.g. C++ this is implementation defined.
See also
Wikipedia/Modulo operation
References
MSDN/C# Language Reference/% operator
That is the Modulo operator. It will give you the remainder of a division operation.
It's the modulus operator. That is, 2 % 2 == 0, 4 % 4 % 2 == 0 (2, 4 are divisible by 2 with 0 remainder), 5 % 2 == 1 (2 goes into 5 with 1 as remainder.)
It is the modulo operator. i.e. it the remainder after division 1 % 36 == 1 (0 remainder 1)
That is the modulo operator, which finds the remainder of division of one number by another.
So in this case a will be the remainder of b divided by c.
It's is modulus, but you example is not a good use of it. It gives you the remainder when two integers are divided.
e.g. a = 7 % 3 will return 1, becuase 7 divided by 3 is 2 with 1 left over.
It is modulus operator
using System;
class Test
{
static void Main()
{
int a = 2;
int b = 6;
int c = 12;
int d = 5;
Console.WriteLine(b % a);
Console.WriteLine(c % d);
Console.Read();
}
}
Output:
0
2
is basic operator available in almost every language and generally known as modulo operator.
it gives remainder as result.
Okay well I did know this till just trying on a calculator and playing around so basically:
5 % 2.2 = 0.6 is like saying on a calculator 5/2.2 = 2.27 then you multiply that .27 times the 2.27 and you round and you get 0.6. Hope this helps, it helped me =]
Nobody here has provided any examples of exactly how an equation can return different results, such as comparing 37/6 to 37%6, and before some of you get upset thinking that you did, pause for a moment and think about it for a minute, according to Dirk Vollmar in here the int x % int y parses as (x - (x / y) * y) which seems fairly straightforward at first glance, but not all Math is performed in the same order.
Since not every equation has it's proper brackets, some Schools will teach that the Equation is to be parsed as ((x - (x / y)) * y) whilst other Schools teach (x - ((x / y) * y)).
Now I experimented with my example (37/6 & 37%6) and figured out which selection was intended (it's (x - ((x / y) * y))), and I even displayed a nicely built if Loop (even though I forgot every End of Line Semicolon) to simulate the Divide Equation at the most Fundamental Scale, as that was in fact my point, the Equation is similar, yet Fundamentally different.
Here's what I can remember from my deleted Post (this took me around an Hour to Type from my Phone, indents are Double Spaced)
using System;
class Test
{
static void Main()
{
float exact0 = (37 / 6); //6.1666e∞
float exact1 = (37 % 6); //1
float exact2 = (37 - (37 / 6) * 6); //0
float exact3 = ((37 - (37 / 6)) * 6); //0
float exact4 = (37 - ((37 / 6) * 6)); //185
int a = 37;
int b = 6;
int c = 0;
int d = a;
int e = b;
string Answer0 = "";
string Answer1 = "";
string Answer2 = "";
string Answer0Alt = "";
string Answer1Alt = "";
string Answer2Alt = "";
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d + ", e: " + e);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.WriteLine("Init Complete, starting Math...");
Loop
{
if (a !< b) {
a - b;
c +1;}
if else (a = b) {
a - b;
c +1;}
else
{
String Answer0 = c + "." + a; //6.1
//this is = to 37/6 in the fact that it equals 6.1 ((6*6=36)+1=37) or 6 remainder 1,
//which according to my Calculator App is technically correct once you Round Down the .666e∞
//which has been stated as the default behavior of the C# / Operand
//for completion sake I'll include the alternative answer for Round Up also
String Answer0Alt = c + "." + (a + 1); //6.2
Console.WriteLine("Division Complete, Continuing...");
Break
}
}
string Answer1 = ((d - (Answer0)) * e); //185.4
string Answer1Alt = ((d - (Answer0Alt)) * e); // 184.8
string Answer2 = (d - ((Answer0) * e)); //0.4
string Answer2Alt = (d - ((Answer0Alt) * e)); //-0.2
Console.WriteLine("Math Complete, Summarizing...");
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.Read();
}
}
This also CLEARLY demonstrated how an outcome can be different for the exact same Equation.
In the code below it should multiply 2 numbers. It works for 3 and less than 3 digits numbers but when I give numbers with 4 digits or bigger it gives runtime error: stackoverflow exception was unhandled. I've commented where the problem is. I thought the problem is for defining variables in int and changed them in long but the problem still exists. Where is the mistake?
edited:
now,whats do you think about the problem?it doesnt do anything
public long Prod2(long u, long v)
{
var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
int n = Math.Max((int)Math.Floor(Math.Log10(u) + 1),(int)Math.Floor(Math.Log10(v) + 1));
int threshold = 3;
if (u == 0 || v == 0)
{
return 0;
}
else if (n <= threshold)
{
return u * v;
}
else
{
int m = (int)Math.Ceiling(n / 2.0);
int x = (int)(u / Math.Pow(10, m));
int y = (int)(u % Math.Pow(10, m));
int w = (int)(u / Math.Pow(10, m));
int z = (int)(v % Math.Pow(10, m));
long r = Prod2(x + y, w + z);
long p = Prod2(x, w);
long q = Prod2(y, z);
return p * (long)Math.Pow(10, 2 * m) + (r - p - q) * (long)Math.Pow(10, m) + q;
long result = Prod2(numbers[0], numbers[1]);
textBox1.Text = result.ToString();
}
}
You are getting into an infinite recursive loop at this point
long result = bigzarb(x, w) * Math.Pow(10, m) + (bigzarb(x, w) + bigzarb(w, y)) * Math.Pow(10, m) + bigzarb(y, z);///here
textBox1.Text = result.ToString();
I notice this line is executed only when intn > 3, so perhaps you have a logic bug there?
Update: After reading your comments I can see that this test is intended to say "if the length of this string is <= 3 then..." when in fact as written it is actually saying "if the VALUE of this converted string is <= 3 then..."
EDIT: I've completely translated the algorithm described in the book for you:
public long Prod2(long u, long v)
{
int n = Math.Max((int)Math.Floor(Math.Log10(u) + 1), (int)Math.Floor(Math.Log10(v) + 1));
int threshold = 3;
if(u == 0 || v == 0)
{
return 0;
}
else if(n <= threshold)
{
return u * v;
}
else
{
int m = (int)Math.Ceiling(n / 2.0);
int x = (int)(u / Math.Pow(10, m));
int y = (int)(u % Math.Pow(10, m));
int w = (int)(u / Math.Pow(10, m));
int z = (int)(v % Math.Pow(10, m));
long r = Prod2(x + y, w + z);
long p = Prod2(x, w);
long q = Prod2(y, z);
return p * (long)Math.Pow(10, 2 * m) + (r - p - q) * (long)Math.Pow(10, m) + q;
}
}
To get the right result, you'd call this method from some other method like this:
void Main()
{
// Call the method and store the result in variable 'r'.
long r = Prod2(1234, 5678);
Console.WriteLine(r);
/////////////////////////////////
//
// OR - In your case read from textBox7 and then store the result in textBox1
//
/////////////////////////////////
var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
long result = prod2(numbers[0], numbers[1]);
textBox1.Text = result.ToString();
}
So in your event handler, for example for button1, you'd do this to make the call:
public void button1_Click()
{
var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
long result = prod2(numbers[0], numbers[1]);
textBox1.Text = result.ToString();
}
Don't modify the Prod2 that I have, and just paste it with your code. This way, Prod2 does the calculation and then your button1_Click controls the input and what to do with the output.
In a nutshell you have a potential (varying on input) case of:
function bigzarb()
{
bigzarb()
}
so long as the numbers in textBox7 are > 3, i.e. an unclosed recursion loop, which will inevitably become a stackoverflow.
Put a breakpoint on the line in question and you'll quickly see the problem. Without knowing what your method does (I don't recognise the algorithm) I can't help much about cleaning it up, but the first step might be to have a get out clause to return from the function conditionally. However, I also see you overwriting the input arguments u and v before they get used so perhaps you've made a mistake in the algo?
Pow method returns double, so I think your x, y, z, w, and z should be declared as double as well.
you are getting "StackoverFlow" for the Recursive call. its better to mold the holes found in the code. I suggest you to change the logic.
static int callStack = 0;
public double bigzarb(long u, long v)
{
callStack++;
............
............
I have next code
int a,b,c;
b=1;
c=36;
a=b%c;
What does "%" operator mean?
It is the modulo (or modulus) operator:
The modulus operator (%) computes the remainder after dividing its first operand by its second.
For example:
class Program
{
static void Main()
{
Console.WriteLine(5 % 2); // int
Console.WriteLine(-5 % 2); // int
Console.WriteLine(5.0 % 2.2); // double
Console.WriteLine(5.0m % 2.2m); // decimal
Console.WriteLine(-5.2 % 2.0); // double
}
}
Sample output:
1
-1
0.6
0.6
-1.2
Note that the result of the % operator is equal to x – (x / y) * y and that if y is zero, a DivideByZeroException is thrown.
If x and y are non-integer values x % y is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / y (more details in the C# 4.0 Specification in section 7.8.3 Remainder operator).
For further details and examples you might want to have a look at the corresponding Wikipedia article:
Modulo operation (on Wikipedia)
% is the remainder operator in many C-inspired languages.
3 % 2 == 1
789 % 10 = 9
It's a bit tricky with negative numbers. In e.g. Java and C#, the result has the same sign as the dividend:
-1 % 2 == -1
In e.g. C++ this is implementation defined.
See also
Wikipedia/Modulo operation
References
MSDN/C# Language Reference/% operator
That is the Modulo operator. It will give you the remainder of a division operation.
It's the modulus operator. That is, 2 % 2 == 0, 4 % 4 % 2 == 0 (2, 4 are divisible by 2 with 0 remainder), 5 % 2 == 1 (2 goes into 5 with 1 as remainder.)
It is the modulo operator. i.e. it the remainder after division 1 % 36 == 1 (0 remainder 1)
That is the modulo operator, which finds the remainder of division of one number by another.
So in this case a will be the remainder of b divided by c.
It's is modulus, but you example is not a good use of it. It gives you the remainder when two integers are divided.
e.g. a = 7 % 3 will return 1, becuase 7 divided by 3 is 2 with 1 left over.
It is modulus operator
using System;
class Test
{
static void Main()
{
int a = 2;
int b = 6;
int c = 12;
int d = 5;
Console.WriteLine(b % a);
Console.WriteLine(c % d);
Console.Read();
}
}
Output:
0
2
is basic operator available in almost every language and generally known as modulo operator.
it gives remainder as result.
Okay well I did know this till just trying on a calculator and playing around so basically:
5 % 2.2 = 0.6 is like saying on a calculator 5/2.2 = 2.27 then you multiply that .27 times the 2.27 and you round and you get 0.6. Hope this helps, it helped me =]
Nobody here has provided any examples of exactly how an equation can return different results, such as comparing 37/6 to 37%6, and before some of you get upset thinking that you did, pause for a moment and think about it for a minute, according to Dirk Vollmar in here the int x % int y parses as (x - (x / y) * y) which seems fairly straightforward at first glance, but not all Math is performed in the same order.
Since not every equation has it's proper brackets, some Schools will teach that the Equation is to be parsed as ((x - (x / y)) * y) whilst other Schools teach (x - ((x / y) * y)).
Now I experimented with my example (37/6 & 37%6) and figured out which selection was intended (it's (x - ((x / y) * y))), and I even displayed a nicely built if Loop (even though I forgot every End of Line Semicolon) to simulate the Divide Equation at the most Fundamental Scale, as that was in fact my point, the Equation is similar, yet Fundamentally different.
Here's what I can remember from my deleted Post (this took me around an Hour to Type from my Phone, indents are Double Spaced)
using System;
class Test
{
static void Main()
{
float exact0 = (37 / 6); //6.1666e∞
float exact1 = (37 % 6); //1
float exact2 = (37 - (37 / 6) * 6); //0
float exact3 = ((37 - (37 / 6)) * 6); //0
float exact4 = (37 - ((37 / 6) * 6)); //185
int a = 37;
int b = 6;
int c = 0;
int d = a;
int e = b;
string Answer0 = "";
string Answer1 = "";
string Answer2 = "";
string Answer0Alt = "";
string Answer1Alt = "";
string Answer2Alt = "";
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d + ", e: " + e);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.WriteLine("Init Complete, starting Math...");
Loop
{
if (a !< b) {
a - b;
c +1;}
if else (a = b) {
a - b;
c +1;}
else
{
String Answer0 = c + "." + a; //6.1
//this is = to 37/6 in the fact that it equals 6.1 ((6*6=36)+1=37) or 6 remainder 1,
//which according to my Calculator App is technically correct once you Round Down the .666e∞
//which has been stated as the default behavior of the C# / Operand
//for completion sake I'll include the alternative answer for Round Up also
String Answer0Alt = c + "." + (a + 1); //6.2
Console.WriteLine("Division Complete, Continuing...");
Break
}
}
string Answer1 = ((d - (Answer0)) * e); //185.4
string Answer1Alt = ((d - (Answer0Alt)) * e); // 184.8
string Answer2 = (d - ((Answer0) * e)); //0.4
string Answer2Alt = (d - ((Answer0Alt) * e)); //-0.2
Console.WriteLine("Math Complete, Summarizing...");
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.Read();
}
}
This also CLEARLY demonstrated how an outcome can be different for the exact same Equation.