Switch to full style
For C/C++ coders discussions and solutions
Post a reply

Pseudo Randon Number Generator code??

Sat Apr 11, 2009 2:35 am

Hi,

I'm trying to use a simple PRNG C program but am having problems.

Could someone please run this code and let me know if it works and post the output. It should be working perfectly but I am having trouble with my compiler. Thanks


Code:


#include<stdio.h>
#include<stdint.h>

uint16_t rand16();
uint32_t rand32();

uint16_t n, userChosenValue = 0; //Change this for different seed values.
uint16_t numberOfValues = 10; //Change this depending on your needs.
uint32_t seed;
uint32_t mlcg,p,q;
uint64_t tmpseed;

int main(){
  
  
/* Calculate and print a series of 16 bit random numbers */
  seed = (uint32_t)(userChosenValue + 1); 
  
  printf
("16 Bit:\n\n");
  for (n=0;n<numberOfValues;n++){
    printf("%.4x\n",rand16());
  }
  
  
/* Calculate and print a series of 32 bit random numbers */
  seed = (uint32_t)(userChosenValue + 1); 
  
  printf
("\n\n32 Bit:\n\n");
  for (n=0;n<numberOfValues;n++){
    printf("%.8x\t\t\n",rand32());
  }
    
  return 0
;
}

/* Return the next 32 bit random number */
uint32_t rand32() {
    
  tmpseed 
=  (uint64_t)33614U * (uint64_t)seed;
  q = tmpseed;     /* low */
  q = q >> 1;
  p = tmpseed >> 32 ;        /* hi */
  mlcg = p + q;
  if (mlcg & 0x80000000) { 
    mlcg 
= mlcg & 0x7FFFFFFF;
    mlcg++;
  }
  seed = mlcg;
  
  return mlcg
; 
}

/* Return low 16 bits of next 32 bit random number */
uint16_t rand16() {
  return (uint16_t)rand32();
}
 




Post a reply
  Related Posts  to : Pseudo Randon Number Generator code??
 Factor of number using C++ code     -  
 code to find a number all divisors using recursion     -  
 How to write a code for sorting array of 100 number in C++     -  
 convert integer number to octal,hexadecimal number systems     -  
 Sequence Generator JPA     -  
 Histogram Generator     -  
 Calendar Generator     -  
 convert decimal number to octal number     -  
 convert octal number to decimal number     -  
 JPA entity with table generator     -