# Pointer arithmetic to void* (uintptr_t)

The link below is a very important one regarding pointer arithmetic, a subject that frequently is being taught incorrectly in many universities:

https://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c

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

Cast the pointer to uintptr_t and make any arithmetic operation you want. Then cast the uintptr_t back to the type of the pointer you want:

```void calculateModulo(char *address) {
x = x % 4;
char * resultingaddress = (char*) x;
....
}```

# 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));`

### Summary

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

# Netbeans C/C++ template with Valgrind and -Wall

Netbeans is a great free IDE, one I recommend to all beginners.

For C/C++ coding, it has one important drawback though: -Wall is not selected by default, which is something absolutely required for beginners.

For this reason, I have created my own template (-wall, run at external terminal, valgrind configuration etc) which is available here

In the following page you can find how to create and use one of your own:

https://blogs.oracle.com/seapegasus/entry/tip_create_your_own_project

Basically you point the IDE to an existing NetBeans project and tell it to use that as a template. You use a module project as a wrapper: This will allow you to turn the template project into an NBM file (“NetBeans Module”) that can be installed into the IDE like a plugin. After installing the custom plugin, the template project will appear in the “New Project” wizard. This is how you do it:

1. Take a NetBeans project that you want to be the template project, and open it in the IDE. Let’s call it FooApp.
2. Create an empty NetBeans Module project. Name it something recognizable (such as FooAppTemplate or whatever).
3. In the Projects window, right-click the empty module project. Create a new file by choosing New > Other > Module Development > Project Template from the context menu.
4. Use the Template Wizard to configure the template. Point it to the template project FooApp and choose a category (Java SE, Java EE, Java ME, Ruby, etc).
5. Here comes the important part – you save the template as an NBM file. Right-click the module project in the Projects window, and select Create NBM. Look into the Output window to see where the file was saved (for instance FooAppTemplate/build/myorg-fooapptemplate.nbm).
6. To install the template into the IDE, go to Tools > Plugins > Downloaded. Click Add Plugin, browse to your NBM file and install it. If you give the NBM file to other developers, that’s how they install the template.
7. (Alternatively, right-click the module project in the Project window and select “Install in Development IDE”. This is another way to install it for somebody who has the module project still open.)

# Recommended books for C

#### Top 5: From beginner to expert

A complete list follows.