# 随机数函数向量化

Drand48 Vectorization in C/C++ Goodman, Steve9700.00000000000

double drand48(void);

double erand48(unsigned short xsubi[3]);

long int lrand48(void);

long int nrand48(unsigned short xsubi[3]);

long int mrand48(void);

long int jrand48(unsigned short xsubi[3]);

1. drand48 向量化。

#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[3] = {155,0,155};

// Initialize Seed Value For Random Number

seed48(&seed[0]);

for (i = 0; i < ASIZE; i++){

rand_number[i] = drand48();

}

// Sample Array Element

printf("%f\n", rand_number[ASIZE-1]);

return 0;

}

2. erand38 向量化种子值作为参数传输。

#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[3] = {155,0,155};

#pragma ivdep

for (i = 0; i < ASIZE; i++){

rand_number[i] = erand48(&seed[0]);

}

// Sample Array Element

printf("%f\n", rand_number[ASIZE-1]);

return 0;

}

3. lrand38 向量化。

#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[3] = {155,0,155};

// Initialize Value For Random Number

seed48(&seed[0]);

for (i = 0; i < ASIZE; i++){

rand_number[i] = lrand48();

}

// Sample Array Element

printf("%ld\n", rand_number[ASIZE-1]);

return 0;

}

4. nrand48 向量化种子值作为参数传输。

#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[3] = {155,0,155};

#pragma ivdep

for (i = 0; i < ASIZE; i++){

rand_number[i] = nrand48(&seed[0]);

}

// Sample Array Element

printf("%ld\n", rand_number[ASIZE-1]);

return 0;

}

5. mrand48 向量化。

#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[3] = {155,0,155};

// Initialize Seed Value For Random Number

seed48(&seed[0]);

for (i = 0; i < ASIZE; i++){

rand_number[i] = mrand48();

}

// Sample Array Element

printf("%ld\n", rand_number[ASIZE-1]);

return 0;

}

6. jrand48 向量化种子值作为参数传输。

#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[3] = {155,0,155};

#pragma ivdep

for (i = 0; i < ASIZE; i++){

rand_number[i] = jrand48(&seed[0]);

}

// Sample Array Element

printf("%ld\n", rand_number[ASIZE-1]);

return 0;

}

Fortran 支持

RANF()

RANDOM_NUMBER() single precision

RANDOM_NUMBER() double precision

Para obter informações mais completas sobre otimizações do compilador, consulte nosso aviso de otimização.
Categorias: