# wichmann-hill algorithm

Discussion in 'Programming' started by weezy, Aug 11, 2009.

1. ### weezyNew Member

Joined:
Aug 11, 2009
Messages:
2
0
Trophy Points:
0
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.

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 a moderator: Aug 12, 2009
2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
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
```

3. ### weezyNew Member

Joined:
Aug 11, 2009
Messages:
2
0
Trophy Points:
0
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

4. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
> 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?

Joined:
Jun 14, 2009
Messages:
817