0

So I'm reading an AI book and it is talking about Gaussian Randomness and it includes this example code

unsigned long seed = 61829450;
double GaussianRand()
{
  double sum = 0;
  for (int i = 0: i < 3; i++)
    {
       unsigned long holdseed = seed;
       seed^= seed << 13;
       seed^= seed >> 17;
       seed^= seed << 5;
       long r = (Int64)(holdseed + seed);
       sum += (double)r * (1.0/0x7FFFFFFFFFFFFFFF);
    }
  return sum; //returns [-3.0, 3.0] at (66.7%, 95.8%, 100%)

So taking some of the knowledge I have learned from using C++ here at my University, I did this with it.

#include <iostream>
#include <random>
#include <inttypes.h>

using namespace std;

double GaussianRand();

int main()
{
  GaussianRand();
}

double GaussianRand()
{
  double sum = 0;
  for (int i = 0; i < 3; i++)
    {
      unsigned long holdseed = seed;
      seed^= seed << 13;
      seed^= seed >> 17;
      seed^= seed << 5;
      long r = (Int64)(holdseed + seed);
      sum += (double)r * (1.0/0x7FFFFFFFFFFFFFFF);
    }
  return sum;
}

I am running into the problem of not understanding what Int64 is or how I am supposed to be using it. I have googled some things and I think I am just confusing myself on this since I have not actually seen this before or learned it. Is int64_t the same thing as Int64 or are they two completely different thing?

------------------THIS IS AN UPDATE I PROMISED YESTERDAY------------------------

So after a bit of playing around I found out that to use Int64 I have to use the

using namespace System;

line of code. To do this I had to create a project that was a CLR Console Application and just stick the code in there. The code looks like this now

#include "stdafx.h"
#include <iostream>
#include <random>

using namespace System;
using namespace std;

unsigned long seed = 61829450;
double GaussianRand();

int main()
{
 GaussianRand();
}

double GaussianRand()
{
 double sum = 0;
 for (int i = 0; i < 3; i++)
 {
  unsigned long holdseed = seed;
  seed ^= seed << 13;
  seed ^= seed >> 17;
  seed ^= seed << 5;
  long r = (Int64)(holdseed + seed);
  sum += (double)r * (1.0 / 0x7FFFFFFFFFFFFFFF);
  cout << sum << endl;
 }
 cout << sum << endl;
 return sum;  /// returns [-3.0, 3.0] at [67.7%, 95.8%, 100%]
}

It compiles and runs without any problems.

Thank you to everyone who helped me figure this out

ShadowManes
  • 68
  • 10
  • 3
    `Int64` is not a standard C++ type, [`int64_t`](http://en.cppreference.com/w/cpp/types/integer) is. It should be the same thing though, a 64-bit signed integer type. There is a [`Int64`](https://msdn.microsoft.com/en-us/library/system.int64%28v=vs.110%29.aspx) *structure* in .NET, maybe the code in the book is using .NET? – Some programmer dude Jun 22 '15 at 14:41
  • I have not found Int64, is there any typedef or lib that you are using and not in question? – sop Jun 22 '15 at 14:43
  • @JoachimPileborg the book only said it was C++ code so it is possible I guess – ShadowManes Jun 22 '15 at 14:45
  • @sop All of the code provided is exactly how it appears in the book. With the exception of the part I updated to make it actually work – ShadowManes Jun 22 '15 at 14:46
  • 1
    There is an `Int64` in .NET (C++, C#, ...), take a look [here](https://msdn.microsoft.com/en-us/library/system.int64(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1). MSDN says it "represents a 64-bit signed integer". – triple_r Jun 22 '15 at 14:47
  • 1
    `seed^= << 13;`: That ain't C++! What does `^= <<` mean? – TonyK Jun 22 '15 at 14:49
  • @JoachimPileborg I will try to go to Visual Studio and run it that way and see if it works. I'll update this at a later time if it does – ShadowManes Jun 22 '15 at 14:55
  • @triple_r I will try to go to Visual Studio and run it that way and see if it works. I'll update this at a later time if it does – ShadowManes Jun 22 '15 at 14:55
  • @TonyK the first code snippet says `seed^= seed << 13;` and etc. instead of `seed^= << 13;` so I'm guessing it was a typo in the second snippet. – triple_r Jun 22 '15 at 14:58
  • @triple_r yea that was a typo on my part there. My edited code has the seed^= seed << 13 on it. I should fix that really quick to avoid further confusion – ShadowManes Jun 22 '15 at 14:59
  • You were too slow, Shadow -- I saw your comment! Don't worry, I certainly won't be responding to any of your questions again. – TonyK Jun 22 '15 at 15:03
  • @ShadowManes it happens, I guessed because you were getting error on `Int64` line, you didn't check the rest of the code for errors and stuff. No worries :-) – triple_r Jun 22 '15 at 15:06
  • @TonyK It's cool. We all make mistakes. Live and learn on my part – ShadowManes Jun 22 '15 at 15:06

1 Answers1

0

Int64 is not a standard C++. It's not hard to see that there is no workaround which makes int == int32_t == long on 32-bit systems. For the same reason, there's no way to make long == int64_t == long long on 64-bit systems.

Add this include to your project:

#include <inttypes.h>

Then use uint64_t or int64_t.

what is the difference between <stdint.h> and <inttypes.h>?

<inttypes.h> includes <stdint.h> and just adds more macros/functions. So both should be fine.

enter image description here