# Random Number Function Vectorization

Published:09/07/2012   Last Updated:03/08/2019

## Vectorization Essentials, Random Number Function Vectorization

Beginning with the Intel Compiler version 13.0 random number auto- vectorization of the drand48 family of random number functions in C/C++ and RANF and Random_Number functions in Fortran is supported. Vectorization is supported through the Intel Short Vector Math Library (SVML).

Supported C/C++ Functions:

``````double drand48(void);
double erand48(unsigned short xsubi);
long int lrand48(void);
long int nrand48(unsigned short xsubi);
long int mrand48(void);
long int jrand48(unsigned short xsubi);``````

### Simple Examples:

#### 1. drand48 vectorization.

``````#include <stdlib.h>

#include <stdio.h>
#define ASIZE 1024

int main(int argc, char *argv[])
{
int i;
double rand_number[ASIZE] = {0};
unsigned short seed = {155,0,155};

// Initialize Seed Value For Random Number
seed48(&seed);

for (i = 0; i < ASIZE; i++){
rand_number[i] = drand48();
}

// Sample Array Element
printf("%f\n", rand_number[ASIZE-1]);
return 0;
}``````

#### 2. erand48 vectorization, seed value is passed as an argument.

``````#include <stdlib.h>

#include <stdio.h>
#define ASIZE 1024

int main(int argc,  char *argv[])
{
int i;
double rand_number [ASIZE] = {0};
unsigned short seed = {155,0,155};

#pragma ivdep
for (i = 0; i < ASIZE; i++){
rand_number[i] = erand48(&seed);
}

// Sample Array Element
printf("%f\n", rand_number[ASIZE-1]);
return 0;
}``````

#### 3. lrand48 vectorization

``````#include <stdlib.h>
#include <stdio.h>
#define ASIZE 1024

int main(int argc, char *argv[])
{
int i;
long rand_number[ASIZE] = {0};
unsigned short seed = {155,0,155};

// Initialize Value For Random Number
seed48(&seed);

for (i = 0; i < ASIZE; i++){
rand_number[i] = lrand48();
}

// Sample Array Element
printf("%ld\n", rand_number[ASIZE-1]);
return 0;
}``````

#### 4. nrand48 vectorization, seed value id passed as an argument.

``````#include <stdlib.h>
#include <stdio.h>
#define ASIZE 1024

int main(int argc,  char *argv[])
{
int i;
long rand_number[ASIZE] = {0};
unsigned short seed = {155,0,155};

#pragma ivdep
for (i = 0; i < ASIZE; i++){
rand_number[i] = nrand48(&seed);
}

// Sample Array Element
printf("%ld\n", rand_number[ASIZE-1]);
return 0;
}``````

#### 5. mrand48 vectorization.

``````#include <stdlib.h>

#include <stdio.h>
#define ASIZE 1024

int main(int argc,  char *argv[])
{
int i;
long rand_number[ASIZE] = {0};
unsigned short seed = {155,0,155};

// Initialize Seed Value For Random Number
seed48(&seed);

for (i = 0; i < ASIZE; i++){
rand_number[i] = mrand48();
}

// Sample Array Element
printf("%ld\n", rand_number[ASIZE-1]);
return 0;
}``````

#### 6. jrand48 vectorization, seed value is passed as an argument.

``````#include <stdlib.h>
#include <stdio.h>
#define ASIZE 1024

int main(int argc,  char *argv[])
{
int i;
long rand_number[ASIZE] = {0};
unsigned short seed = {155,0,155};

#pragma ivdep
for (i = 0; i < ASIZE; i++){
rand_number[i] = jrand48(&seed);
}

// Sample Array Element
printf("%ld\n", rand_number[ASIZE-1]);
return 0;
}``````

### Fortran Support:

For Fortran we support:

``````RANF()

RANDOM_NUMBER() single precision

RANDOM_NUMBER() double precision``````

NEXT STEPS

It is essential that you read this guide from start to finish using the built-in hyperlinks to guide you along a path to a successful port and tuning of your application(s) on Intel® architecture. The paths provided in this guide reflect the steps necessary to get best possible application performance.

Back the main chapter Vectorization Essentials.

#### Product and Performance Information

1

Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.