C Program to Implement Naor-Reingold Pseudo Random Function

This is a C Program to genrate random numbers using Naor-Reingold random function.

Here is source code of the C Program to Implement Naor-Reingold Pseudo Random Function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. int main(int argc, char **argv) {
  7.     int p = 7, l = 3, g = 2, n = 4, x, i, j, k;
  8.     int mul = 1;
  9.     int a[] = { 1, 2, 2, 1 };
  10.     int bin[4];
  11.     printf("The Random numbers are: ");
  12.     for (i = 0; i < 10; i++) {
  13.         x = rand() % 16;
  14.         for (j = 3; j >= 0; j--) {
  15.             bin[j] = x % 2;
  16.             x /= 2;
  17.         }
  18.         for (k = 0; k < 4; k++)
  19.             mul *= pow(a[k], bin[k]);
  20.         printf("%f ", pow(g, mul));
  21.     }
  22. }

Output:

$ gcc Naor-Reingold.c
$ ./a.out
 
The Random numbers are: 2.000000 4.000000 16.000000 4.000000 2.000000 4.000000 16.000000 16.000000 4.000000 2.000000

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.