wichmann-hill algorithm

weezy's Avatar, Join Date: Aug 2009
Newbie Member
Hello,

I'm new with coding c/c++ and uniform random generator. I had to code the wichmann-hill.

I need to generate about 30,000 random numbers, but i dont know how to.

Here is my cpp code so far.

Can anyone please help?
Code:
 
#include <stdio.h>
#include <cstdlib.h>
#include <math.h>
#include <fstream.h>
#include <time.h>
int main (void) {
 int x1, y1, z1, i;
 float random, temp, r;
 srand( time(NULL));
 x1 = (1+ rand()) % 200;
 y1 = (1000 + rand()) % 10000;
 z1 = (300 + rand()) % 3000;
 x1 =171 * (x1 % 177) - 2 * (x1 / 177);
 if (x1 < 0)
 {
  x1 = x1 + 30269;
 }
 y1 =172 * (y1 % 176) - 35 * (y1 / 176);
  if (y1 < 0)
 {
  y1 = y1 + 30307;
 }
 z1 =170 * (z1 % 178) - 63 * (z1 / 178);
  if (z1 < 0)
 {
  z1 = z1 + 30323;
 }
 temp = (float) x1/30269.0 + (float) y1/30307.0 + (float) z1/30323.0;
 random = temp - floor(temp);
 
 
 ofstream outputFile("dolly.txt", ios::out);
 //outputFile << random << endl;
 outputFile.close();
 

 return 0;
}

Last edited by shabbir; 12Aug2009 at 08:47.. Reason: Code blocks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Please use code tags when posting code.

What exactly do you want help with? Does the program not work as you expect; if so, what does it do that you think it shouldn't, or what doesn't it do that you think it should?

Looking at the code it seems to calculate one number and output it to a file. Is this number correct according to the algorithm?

If so then presumably you just need to wrap the code in a for loop, e.g.
Code:
// open file
for (int count=0; count<30000; count++)
{
  // the code that calculates the next number and writes it to a file
}
// close file and exit
0
weezy's Avatar, Join Date: Aug 2009
Newbie Member
How do i use code tags??

Ok let me try to explain:

right now my code is just calculating one number (which is correct according to the algorithm) and outputing it to afile..

but i want it to calculate 3000 numbers and output all these numbers into that file.

That's my dilemma.

Thanks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> How do i use code tags??

It's right there in the posting guidelines. You did read that, didn't you? If not, READ THE POSTING GUIDELINES PLEASE. You should ALWAYS read posting guidelines when you're new to a forum; it's only polite.

> but i want it to calculate 3000 numbers and output all these numbers into that file.

And how does my previous post NOT answer that question?
0
mayjune's Avatar, Join Date: Jun 2009
Invasive contributor
i agree give it a for loop and it should work....try it and say whats the problem...if there is...