Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    Random Numbers

    Hey.
    The intent of this program is to help answer the question “How good is the random number generator rand()?”. The program must be designed and written in a modular fashion using functions and arrays

    The function rand() returns int values between 0 and some system-specified maximum value. The first part of the problem is to discover that maximum value by two different methods: compute it, or just retrieve it. The actual value is given by the constant RAND_MAX which is defined in the header file <cstdlib>.

    The computed value could be estimated by making repeated trials and retaining the largest value seen in those trials, but it’s not clear how many trials you would need to get the right answer. So, the first part of the project is to make 1,000 trials and see how close to RAND_MAX you actually came. This will require a function whose sole parameter is the number of trials.

    The values from rand() are supposed to be uniformly (evenly) distributed between 0 and RAND_MAX. The second part of the project is to test the claim that the values rand()%n are uniformly distributed between 0 and n-1. To do this, count the number of occurrences of each value of rand()%n as you make RAND_MAX trials.

    This will require a loop over the number of trials, and an array to do the counting. Print the array to cout when finished. Do this for n = 2, 10, 16, 100 and 128. This will require a function whose sole parameter is n.

    There are many situations where you need to generate random points in a square. As a simple example, consider two consecutive values a, b obtained from rand(). How often do these combinations occur?
    a and b are both even,
    a is even, b is odd,
    a is odd, b is even,
    a and b are both odd.


    The third part of the project is to test the claim that all four combinations occur equally often. You should make RAND_MAX/2 trials (each trial uses two calls to rand()). This will require a function with no parameters, but you could generalize the test from 2 x 2 to n x n (still considering only two consecutive values).
    Before starting each series of tests, be sure to restart the random number sequence by calling srand(0). This will ensure consistent results.


    Okay;
    Here is what I have;
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int generatehighvalue (int trials);
    void countoccurrence(int trials);
    void main ()
    {
    	int highestgenerated;
    highestgenerated = generatehighvalue(1000);
    cout <<"difference we got is"<<RAND_MAX - highestgenerated<<endl;
    cout<<"hishest possible is"<<RAND_MAX<< "and highest generated is"<<highestgenerated<<endl;
    countoccurence(1);
    }
    ]
    
    int generatehighvalue (int valuetrials)
    [
    	int num, compare;
    	num = rand();
    	compare = 0;
    	if (trials > 1)
    		compare  = generatehighvalue(trials - 1)
    		if (compare > num)
    			return compare;
    		else
    			return num;
    ]
    
    void countoccurence(int maxinum)
    [
    	int occurence[128];
    	memset (occurence,0,sizeof(occurence));
    	if (maxnum > 128)
    	{
    		cout <<"max number cannot be more than 128"<<endl;
    
    		return ;
    	}
    for(int trials = RAND_MAX; trials>0; trials--)
    {
    I dont really understand what to do next... to match the sample output of this;


    We used srand(time(0)) to initialize the calls to rand(). You should
    use srand(0) instead. You should not expect to get the same numbers as
    these, but the totals should agree, and the formatting should be similar.

    Only a few examples are shown here. While you are trying to get the
    output into a nice format, it would be easiest to skip the cases of n =
    100 and 128.

    Some people with access to Linux have gotten very different results.
    In particular, RAND_MAX is much larger there, so the program takes longer
    to run.

    -----------------------------------------------------------------------------

    After 1000 trials, max rand() seems to be 32698
    RAND_MAX = 32767

    Testing rand()%n for n = 2
    0 16219
    1 16548
    missing 0 of 2

    Testing rand()%n for n = 10
    0 3255
    1 3357
    2 3204
    3 3334
    4 3191
    5 3267
    6 3274
    7 3260
    8 3295
    9 3330
    missing 0 of 10

    Testing rand()%n for n = 16
    0 2073
    1 1983
    2 2030
    3 2123
    4 2030
    5 2021
    6 1951
    7 1992
    8 2067
    9 2052
    10 2053
    11 2118
    12 1998
    13 2222
    14 2017
    15 2037
    missing 0 of 16

    count[0][0] = 4009
    count[0][1] = 4107
    count[1][0] = 4094
    count[1][1] = 4173

    count[0][0] = 1826
    count[0][1] = 1794
    count[0][2] = 1860
    count[1][0] = 1803
    count[1][1] = 1818
    count[1][2] = 1875
    count[2][0] = 1805
    count[2][1] = 1818
    count[2][2] = 1784

  2. #2
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45
    Have you even tried to compile? Your code is a mess. Clean it up, compile it and then review it. Crappy code is very confusing... ie brackets ? or did you mean braces ? maxnum or maximum? trials? or valuetrials? Your definitions and references are not solid. If statement with indentation but no braces? an empty for statement?? Help yourself before we can help you.

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Comments will always help us help you...
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    punter,

    You've written far too much code without testing it...

    The secret to successful programming is to write your program in small, manageable, sections. For beginners, this literally means one or two lines at a time. I know it seem ridiculous, but it’s waaaay easier to debug two lines of code than it is to debug 20 lines. Professional programmers use the same technique. They might write a full page of code before testing, but they never write the whole program before trying it out.

    I'm not saying that you should program by trial-and-error, but programming is hard and you need to check your work as you go along.

    So, start-out with a “Hello World” (or “Hello Random”) program. Add a one or two lines, test-compile and test-run. When it works, add one or two more lines. Test-compile and test run. Repeat 'till done!

    This can be a little tricky, because the program has to be complete-enough to compile… you can’t just add lines of code in order line 1, 2, 3, 4... through the end. The program has to appear complete to the compiler. Part of the fun is figuring-out which lines of code to work on next.

    When you are given an assignment like this, work on one requirement at a time. For example, you can write the RAND_MAX part of the program before adding the even-distribution stuff. And when you work on the even-distribution part, work on one of those tests at a time...

    When you create a new function, start out with an empty function. If the function needs to return a value, just make it return a known value (i.e. return 10;). When it seems to be working, you can add the real logic to the function.

    It’s often helpful to stick in some extra temporary cout statements, so that you can “see” what the program is doing before you’re done. (i.e. cout << “compare = “ << compare << endl; )

    If you have enough time, I think you can do this assignment! Just break it down into little parts.
    Last edited by DougDbug; 11-02-2006 at 07:56 PM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Not related to your problems, but your teacher is wrong about one thing:

    The second part of the project is to test the claim that the values rand()%n are uniformly distributed between 0 and n-1
    This only completely holds true if RAND_MAX + 1 is a multiple of n, and approximately holds true if n is much less than RAND_MAX . If you need a more precisely even distribution you should reject any value of rand() that is >= n*(RAND_MAX/n)

    As an absurd example, if RAND_MAX was 5, then rand() % 4 would yield 0 or 1 at double the rates of 2 or 3.

    Of course, for your project that would be overkill, and your teacher might not understand why you'd do it
    Last edited by Cat; 11-03-2006 at 11:42 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> your teacher is wrong about one thing
    I'd imagine that the teacher was wanting to have the students test that claim, not instructing that it is absolutely true.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Quote Originally Posted by DougDbug
    punter,

    You've written far too much code without testing it...

    The secret to successful programming is to write your program in small, manageable, sections. For beginners, this literally means one or two lines at a time. I know it seem ridiculous, but it’s waaaay easier to debug two lines of code than it is to debug 20 lines. Professional programmers use the same technique. They might write a full page of code before testing, but they never write the whole program before trying it out.

    I'm not saying that you should program by trial-and-error, but programming is hard and you need to check your work as you go along.

    So, start-out with a “Hello World” (or “Hello Random”) program. Add a one or two lines, test-compile and test-run. When it works, add one or two more lines. Test-compile and test run. Repeat 'till done!

    This can be a little tricky, because the program has to be complete-enough to compile… you can’t just add lines of code in order line 1, 2, 3, 4... through the end. The program has to appear complete to the compiler. Part of the fun is figuring-out which lines of code to work on next.

    When you are given an assignment like this, work on one requirement at a time. For example, you can write the RAND_MAX part of the program before adding the even-distribution stuff. And when you work on the even-distribution part, work on one of those tests at a time...

    When you create a new function, start out with an empty function. If the function needs to return a value, just make it return a known value (i.e. return 10. When it seems to be working, you can add the real logic to the function.

    It’s often helpful to stick in some extra temporary cout statements, so that you can “see” what the program is doing before you’re done. (i.e. cout << “compare = “ << compare << endl; )

    If you have enough time, I think you can do this assignment! Just break it down into little parts.
    I did what you said (break things down) and It is all working now.
    I hail you my guru from today onwards *hail*

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The secret to successful programming is to write your program in small, manageable, sections. For beginners, this literally means one or two lines at a time. I know it seem ridiculous, but it’s waaaay easier to debug two lines of code than it is to debug 20 lines. Professional programmers use the same technique. They might write a full page of code before testing, but they never write the whole program before trying it out.
    Wow, at least someone said something smart

    I usually make a whole bunch of code and then if I have some problems, I test commenting out large blocks of code or track down the errors with outputting the variables before, in the middle, and after every possible troublemaker.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    All very well, but you still have to make sure you do not comment out the error! Lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM