wichmann-hill algorithm

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

  1. weezy

    weezy New Member

    Joined:
    Aug 11, 2009
    Messages:
    2
    Likes Received:
    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.: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;
    }
     
    Last edited by a moderator: Aug 12, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    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. weezy

    weezy New Member

    Joined:
    Aug 11, 2009
    Messages:
    2
    Likes Received:
    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. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    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?
     
  5. mayjune

    mayjune New Member

    Joined:
    Jun 14, 2009
    Messages:
    814
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pune,Delhi
    i agree give it a for loop and it should work....try it and say whats the problem...if there is...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice