Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/forums/programming-forum/)
-   -   wichmann-hill algorithm (http://www.go4expert.com/forums/wichmann-hill-algorithm-t18992/)

weezy 11Aug2009 22:01

wichmann-hill algorithm
 
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.:confused:

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;
}


xpi0t0s 11Aug2009 22:10

Re: wichmann-hill algorithm
 
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


weezy 11Aug2009 22:16

Re: wichmann-hill algorithm
 
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

xpi0t0s 11Aug2009 23:01

Re: wichmann-hill algorithm
 
> 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?

mayjune 12Aug2009 01:49

Re: wichmann-hill algorithm
 
i agree give it a for loop and it should work....try it and say whats the problem...if there is...


All times are GMT +5.5. The time now is 11:04.