Int64 in some Gaussian Randomness Code

141 views Asked by At

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

1

There are 1 answers

3
Soner from The Ottoman Empire On

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