Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) GetRandomInt64

 

GetRandomInt64 is an integer code snippet to obtain a random int64_t.

 

 

 

 

 

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppGetRandomInt64.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= qt
QMAKE_CXXFLAGS += -std=c++11
SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#ifdef _WIN32
//See http://www.richelbilderbeek.nl/CppCompileErrorSwprintfHasNotBeenDeclared.htm
#undef __STRICT_ANSI__
#endif

#include <cassert>
#include <cinttypes>
#include <cstdlib>

int64_t GetRandomInt64()
{
  static_assert(sizeof(int64_t)  == 8,"int64 has 8 bytes of 8 bits each");
  static_assert(RAND_MAX == 0x7FFF,"Assume RAND_MAX has 15 bits, as 0x7FFF equals 2^15");
  int64_t i = 0;
  i += static_cast<int64_t>(std::rand());
  i <<= 15; //RAND_MAX has 15 bits
  i += static_cast<int64_t>(std::rand());
  i <<= 15; //RAND_MAX has 15 bits
  i += static_cast<int64_t>(std::rand());
  i <<= 15; //RAND_MAX has 15 bits
  i += static_cast<int64_t>(std::rand());
  i <<= 3; //3 bits of the 63 left, use 63 because one bit is used for the sign
  i += static_cast<int64_t>( (std::rand() >> 4) % (1 << 3));
  assert(i >= 0);
  return i;
}

#include <cassert>
#include <iostream>
#include <vector>

int main()
{
  const int shift = 8;
  const int n_categories = (1LL << shift);
  //63 because one bit is used for the sign
  const int64_t divide_by = (1LL << (63 - shift));
  std::vector<int64_t> v(n_categories,0);
  for (int64_t i=0; i!=(1LL << 24); ++i)
  {
    const int64_t x = GetRandomInt64();
    const int index = static_cast<int>(x / divide_by);
    assert(index >= 0);
    assert(index < static_cast<int>(v.size()));
    ++v[index];
  }
  for (int i=0; i!=n_categories; ++i)
  {
    std::cout << v[i] << '\t'; //All these values must be about equal
  }
  std::cout << '\n';
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml