Category Archives: Development

Pointer arithmetic to void* (uintptr_t)

This is a very important link regarding pointer arithmetic that is being taught incorrectly in many universities:

The best way for general arithmetic on pointers for most architectures is to use the uintptr_t  data type:

void calculateModulo(char *address) { 
    uintptr_t x = (uintptr_t)address; 
    uintptr_t y = x % 4;

Upgrade to gcc 6.3+ in Ubuntu

To upgrade gcc to gcc6 use the following commands:

sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-6 -y


To set the new gcc as default use the following:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5

Once you have executed the previous command, g++ may not be available.

So you can execute the following to create a link:

sudo ln -s /usr/bin/g++-5 /usr/bin/g++


Simple SSL configuration for Apache-Tomcat

For the official instructions (Tomcat 8), read this page

The following instructions are for development servers only:

Step 1: Create a keystore file to store the server’s private key and self-signed certificate:

For Windows:

"%JAVA_HOME%\bin\keytool" -genkey -alias tomcat -keyalg RSA

For Unix/Linux:

$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA

You will be asked for various information. Remember the password you typed at this step.

At the end of the procedure, keytool will create a file named “.keystore” to your home directory. By default it is hidden. This file should be moved to Apache-Tomcat home directory and can be renamed (eg to “keystore”).

Step 2: Disable APR:

Edit server.xml and comment out the following line:


Step 3: Activate the SSL connector

Edit server.xml and add (or uncomment and modify) the following lines:

    sslProtocol="TLS" />

You will have you replace “12345678” with your password.

You will also have to replace “keystore” with the full path to the keystore file generated at Step 1.

Step 4: Restart Tomcat

Do not forget to restart Apache-Tomcat



Generate random weighted variables

In this post I describe the common problem how to generate random weighted variables. For example you have a die with 6 faces that is fixed, so the probability of an 1 is not the same with the probability of a 2 etc.

The table below shows an example:

Face Probability
1  25
2  25
3  20
4  20
5  5
6  5

One easy solution is to reduce the problem to another one with a die with 100 faces that is not fixed.

You generate a random value from 0-99 that follows the uniform distribution. Do not use the modulo function to do that. See this post instead.

Once you have that value you perform the following steps:

You check the space within which that value relies.

  1. between 0 and 24, you generate 1 as the weighted variable
  2. between 25 and 49, you generate 2 as the weighted variable
  3. between 50 and 69, you generate 3 as the weighted variable
  4. between 70 and 89, you generate 4 as the weighted variable
  5. between 90 and 94, you generate 5 as the weighted variable
  6. between 95 and 99, you generate 6 as the weighted variable




Generate uniformly distributed random variables

Formulas shown below generate values that follow uniform distribution.

Using simple modulo methods is not considered as a good solution (although used a lot by students) as the module function does not generate all the variables with the same frequency.

Random integer between [ 0, 1 ]

int r = (int) (rand()/(RAND_MAX + 0.0));

Random integer between [ 0, 1 )

int r = (int) (rand()/(RAND_MAX + 1.0));

Random integer between [ 0, N ]

int r = (int) (N * (rand() / (RAND_MAX + 0.0)));

Random integer between [ 0, N )

int r = (int) (N * (rand()/(RAND_MAX + 1.0)));

Random integer between [ M, N ]

int r = (int) (Μ + (rand()/(RAND_MAX + 0.0))*(N-M+1));

Random integer between [ M, N )

int r = (int) (Μ + (rand()/(RAND_MAX + 1.0))*(N-M+1));


The general formula is:

int r = (int) (A + (rand()/(RAND_MAX + C))*B);

Values of M, N, K are shown in the following table:

Range A B  C
 [ 0, 1 ]  0  1  0.0
 [ 0, N ]  0  N  0.0
 [ M, N ]  M  N – M + 1  0.0
 [ 0, 1 )  0  1  1.0
 [ 0, N )  0  N  1.0
 [ M, N )  M  N – M + 1  1.0