Thread: randomly generate numbers

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    30

    randomly generate numbers

    this program draws random lotto numbers using arrays.
    it does make sure the numbers don't repeat.
    I want to know how to write it as a 2D array.

    heres the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define RANGE 50
    #define BALLS 6
    
    int main()
    {
            int numbers[RANGE];
            int c, ball;
            puts("L O T T O  P I C K E R\n");
    
            srandom((unsigned)time(NULL));
    
            /*      initialize array        */
            for(c = 0; c < RANGE; c++)
            numbers[c]=0;
    
            printf("Press Enter to pick numbers: ");
            getchar();
    
            /*      draw numbers    */
            puts("Here they come: ");
            for(c = 0; c < BALLS; c++)
            {
    
            /*      see if a number has already been drawn  */
                    do
                    {
                            ball = random() % RANGE;
                    }
                    while(numbers[ball]);
            /*      Number drawn    */
    
                    numbers[ball] = 1;
                    printf("%d ", ball+1);
            }
    
            printf("\n\nGood luck in the drawing!\n");
            return(0);
    }
    Thanks in advance

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    i don't quit understand how this part works
    Code:
                    do
                    {
                            ball = random() % RANGE;
                    }
                    while(numbers[ball]);

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by maybabier View Post
    this program draws random lotto numbers using arrays.
    it does make sure the numbers don't repeat.
    I want to know how to write it as a 2D array.
    what is it you want to be a 2D array?


    Quote Originally Posted by maybabier View Post
    i don't quit understand how this part works
    Code:
                    do
                    {
                            ball = random() % RANGE;
                    }
                    while(numbers[ball]);
    using the mod operator only gives random numbers from 0 to the number "RANGE" its math(that i dont care about) that isnt worth knowing why, just that it works.
    for example, if you wanted numbers between 5 and 20 you would write it like this

    Code:
    nums = rand() % 15 + 5;
    this makes sure the minimum number is 5 and the max 20. it's magic i guess.
    Last edited by ominub; 05-03-2009 at 12:54 AM. Reason: better douche bag?

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Why is it so hard? You are dividing something by RANGE, whatever the remainder is, will most deffinently be between 0 inclusive and RANGE exclusive. Not a crazy math mess, but 1st grade math.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Referring to srand and rand as srandom and random wont get you any closer to something that compiles, no matter how much nicer you think those names might be.
    Or if you have some #defines not shown here that allow you to use those names, then dont, because you make it harder for everybody.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by valaris View Post
    Why is it so hard? You are dividing something by RANGE, whatever the remainder is, will most deffinently be between 0 inclusive and RANGE exclusive. Not a crazy math mess, but 1st grade math.
    dont be a dick.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    I wanted to see if rand can generate two dimensions like
    numbers [] []
    and make sure they are not repeating. ex: row != row && col != col.
    should i just add another for loop under
    Code:
    for(c = 0; c < BALLS; c++)
    to generate row and col?

  8. #8
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by ominub View Post
    dont be a dick.
    Heh sorry i wasn't trying to be rude, just trying to point out that division isn't magic. Why would you come to an internet forum if you didn't want to learn things.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Why is it so hard? You are dividing something by RANGE, whatever the remainder is, will most deffinently be between 0 inclusive and RANGE exclusive. Not a crazy math mess, but 1st grade math.
    First grade is an exaggeration . I learned division with remainders in 4th grade.

  10. #10
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by maybabier View Post
    Code:
            ball = random() % RANGE;
    Note that rand()%RANGE will yield not-so-random results if RANGE is not a divisor of RAND_MAX.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4
    First of all, computing a number mod A (num % A) gives you a number from 0 to A-1, not 0 to A. Also, in C, mod doesn't work correctly with negative numbers but there's an easy fix and its not important.

    Secondly, its quite simple to extend this to work in 2D. Consider your set to grow so that you have a ROW_RANGE and a COL_RANGE so that your array is numbers[ROW_RANGE][COL_RANGE]. The modified code looks like this:

    Code:
    do
    {
        ball = rand()%(ROW_RANGE*COL_RANGE); // X
    }while(numbers[ball/COL_RANGE][ball%COL_RANGE]); //Y
    Then you set this number to 1 and whatever, you should be able to get the rest.
    Instruction X gives a number between 0 and ROW_RANGE*COL_RANGE - 1
    Instruction Y divides this number by COL_RAGE to get the row and mods it by COL_RANGE to get the column. Work out a simple example and you will see it works. As long as you know how mod works (takes the remainder) then you should be able to understand this.

    I'm feeling less lazy so I will explain more. Imagine that you have a 3 X 4 array.

    0 1 2 3
    4 5 6 7
    8 9 .....


    Element 0 starts the zeroth row, Element 4 starts the first row, Element 8 starts the second row, ..... , Element 4*i starts the ith row. So diving the number by column (=4) will give you the row its in.

    Now notice that element 0 (4,8, ... , 4*j) is in the 0th col, element 1 (5,9, ... , 4*j+1) is in the 1st col, element 2 (6,10, ... , 4*j + 2) is the 2nd col. If you notice the remainder of each of these numbers is the column index its in, so modding the number by col ( = 4) will give you the column its in.


    Now if you want to write better code then that busy while loop isn't necessary.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define RANGE 50
    #define BALLS 50
    
    
    int main()
    {
            int numbers[RANGE];
            int c, ball;
            puts("L O T T O  P I C K E R\n");
    
            srandom((unsigned)time(NULL));
    
            /*      initialize array        */
            for(c = 0; c < RANGE; c++)
            numbers[c]=c+1;
    
            printf("Press Enter to pick numbers: ");
            getchar();
    
            /*      draw numbers    */
            puts("Here they come: ");
            for(c = 0; c < BALLS; c++)
            {
                   ball = rand() % (RANGE - c);
                   printf("%d ", numbers[ball]);
                   numbers[ball] = numbers[RANGE - c -1];
            }
    
            printf("\n\nGood luck in the drawing!\n");
            return(0);
    }
    Here's smarter code that I will not explain in detail. It revolves around only selecting numbers from a set of numbers not chosen already though. That's what this line: numbers[ball] = numbers[RANGE - c -1]; allows you to do. Define both range and balls to 1 million in both your code and the code I posted and compare run times.



    I admittedly don't remember how rand() works since I only use drand48(). I'm also too lazy to look it up but the concept is right if rand() generates large numbers.
    Last edited by void_t; 05-06-2009 at 01:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. a very large interval of rundom numbers
    By molistok in forum C++ Programming
    Replies: 19
    Last Post: 08-03-2006, 03:58 PM
  4. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 01:51 AM
  5. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM