C code for Von Neumann Middle Square

C codes

  • First we write a C code to get one random number using Von Neumann method
  • We choose a random number of desired digit using rand() and then apply Von Neumann method.
  • Then just by some changes we get a code for generating any number of random numbers.

    The codes are as follows:

    Code for generating one random number:

    
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    /*Von Neumann Algorithm*/
    /*Programmers: Gursharn, Smruti, Shwetab, Sagnika, Kaustav*/
    /*Date: Aug 14, 2010*/
    
    unsigned long int randm(int n);
    unsigned long int von(unsigned long int x, int n);
    
    int main(void)
    {
      unsigned long int x, s;
      int n;
      printf("Enter the number of digits in the seed value ");
      scanf("%d",&n);
      if (n >= 10){
        printf("TOO LARGE!!");
        exit(0);
      }
      x = randm(n);
      printf("\nRandom Number generated: %lu\n",von(x,n));
      return 0;
    }
    
    
    /*Generating Random Number of desired digit*/
    
    unsigned long int randm(int n)
    {
      double x;
      unsigned long int y;
      srand(getpid());
      x = rand()/(double)RAND_MAX;
      y =(int) (x * pow(10.0,(double) n));
      return y;
    }
    
    
    /*Calculating Random Number By Von Neumann method*/
    
    unsigned long int von(unsigned long int x, int n)
    {
      unsigned long int y;
      int k;
      k = n/2;
      y = (int)((x/pow(10.0, k * 1.0)) * x);
      y = y % (int) (pow(10.0, n * 1.0));
      return y;
    }
    

    Code for generating sequence of random number:

    
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    /*Von Neumann Algorithm To Generate Several Random Numbers*/
    /*Programmers: Gursharn, Smruti, Shwetab, Sagnika, Kaustav*/
    /*Date: Aug 14, 2010*/
    
    unsigned long long int randm(int n);
    unsigned long long int von(unsigned long long int x, int n);
    
    int main(void)
    {
      unsigned long long int x, s;
      int n, i, r;
    
      printf("Enter the number of digits in the seed value ");
      scanf("%d",&n);
      
      printf("Enter the total number of random numbers to be generated "); 
      scanf("%d",&r);
    
      if (n >=12){
        printf("TOO LARGE!!");
        exit(0);
      }
      
      x = randm(n);
      for(i = 0; i < r; i++){	
         s = von(x,n);
         x = s;
      printf("\nRandom Number generated: %lld\n",s);
      }
      return 0;
    }
    
    
    /*Generating Random Number of desired digit*/
    
    unsigned long long int randm(int n)
    {
      double x;
      unsigned long long int y;
      srand(getpid());
      x = rand()/(double)RAND_MAX;
      y =(unsigned long long int) (x * pow(10.0, n*1.0));
      return y;
    }
    
    
    /*Calculating Random Number By Von Neumann method*/
    
    unsigned long long int von(unsigned long long int x, int n)
    {
      unsigned long long int y;
      int k;
      k = n/2;
      y =(unsigned long long int)((x/pow(10.0, k * 1.0)) * x) % (unsigned long long int) (pow(10.0, n * 1.0));
      return y;
    }
    
    

    R Codes for Von Neuman Middle Square

  • First we select a random number of desired digit using runif().
  • Using this as a seed value we generate a sequence of random numbers.
  • The digits of these numbers are seperated out and then their relative frequencies are plotted.
  • 
    
    #von neumann algorithm
    
    von <- function(n){
      m <- floor(runif(1) * 10^n)
      k <- m * m
      i <- floor(n/2)
      t <- (k %/% (10^i)) %% (10^n)
      t
    }
    
    
    digit.sep <- function(x){
      n = nchar(x)
      x <- as.numeric(x)
      y <- rep(0, n)
      for(i in 1: n)
        y[i] = (x %/% (10 ^(n-i))) - 10* (x %/% (10^(n-i+1) )) 
      y
    }
    
    plot.von <- function(n){
      x <- matrix(0, nrow= n, ncol = 5)
      for(i in 1:n){
        t <- von(5)
        x[i, (6 - nchar(t)):5] <- digit.sep(t)
      }
      a <- rep(0,10)
      
      for(i in 1:10)
        a[i] = sum(x == (i-1))
      a = a/(n * 5)
      barplot(a, xlab = "digits", ylab = "Relative Frequency")
    }