Like Tree1Likes

Distribution of Fix numbers

This is a discussion on Distribution of Fix numbers within the C Programming forums, part of the General Programming Boards category; Alright, so what I'm trying to do is write a code that distributes 500 votes among 3 candidates. How am ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    21

    Distribution of Fix numbers

    Alright, so what I'm trying to do is write a code that distributes 500 votes among 3 candidates. How am I supposed 2 do that.
    I tried distributing random numbers among 3 but that didn't worked well, well not the way I thought it would.


    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
            int n1 = 0, n2 = 0, votes = 500;
            srandom( (unsigned) time(NULL) );
    
    
            n1 = random( ) % votes ;  
            n2 = random( ) % votes ; 
    
            printf("\nNumber 1   Number 2\n");
            printf("\n%d        %d\n", n1, n2);
    
    
            return (0);
    }
    Well the program is just a draft I tried, but.... So any help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    How do you intend to distribute 500 votes among 3 candidates?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    Randomly. After I'm done with the code the user will execute the program and will find the 3 candidates side by side with their randomly generated votes (500 votes distributed among 3) But i cant figure out how to distribute those 500 votes among 3.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by newbi
    Randomly. After I'm done with the code the user will execute the program and will find the 3 candidates side by side with their randomly generated votes (500 votes distributed among 3) But i cant figure out how to distribute those 500 votes among 3.
    Do you just need a count of votes for each candidate, or do you actually need to individually tag each vote (e.g., by numbering them) and then list them along with the candidates?

    By the way, the standard library's PRNG related functions are named srand and rand, not srandom and random, though these may be available outside of the standard library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    Quote Originally Posted by laserlight View Post
    Do you just need a count of votes for each candidate, or do you actually need to individually tag each vote (e.g., by numbering them) and then list them along with the candidates?

    By the way, the standard library's PRNG related functions are named srand and rand, not srandom and random, though these may be available outside of the standard library.
    Naaa just show how much each candidate got among 500 votes. U know its like he final result of an election. U show side by side what each candidate got among a certain # of voters.
    e,g. Candidate A Candidate B Candidate C
    150 100 250
    Like that

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    In that case, it is a matter of having an array of 3 elements (or 3 separate variables) to keep track of each candidate's score. Then, you loop 500 times, using the random number generated to decide which score to increment.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    So u have a separate variable from each candidate and you randomly generate vote for each candidate 500 times and at the end just add which candidate got what number of total votes. I feel so lost. An example code would b friendly as I have no clue.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Here's a hint: You're currently generating two votes going towards 500 candidates.
    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"

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your first random number needs to be between 0 and 500 (or 1 and 498 if all candidates must get at least 1 vote).

    Your second random number needs to be between 0/1 and (500-first random number). It can't be between 0 and 500 again, or you might go well beyond 500 votes.

    Your third number of votes is then equal to 500 - first random number - second random number. So it's not random at all. It's just a calculation.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by newbi
    you randomly generate vote for each candidate 500 times
    No, you randomly generates votes 500 times. The random number generated on each time would be used to determine which candidate gets the vote for that time.

    Imagine if you had 500 voters in a queue to place their votes. Each of them has a ballot slip with three names, and they mark their choice as one of the three names. The voters go to the ballot box one by one to place their ballot slip. Their votes are then tallied, and who voted for who is kept secret: only the totals are announced. What you are doing here simulates this, except that instead of tallying the votes after everyone has voted, you keep a running total for each candidate (and generate a random number instead of getting people to mark their choices).

    Quote Originally Posted by Adak
    Your first random number needs to be between 0 and 500 (or 1 and 498 if all candidates must get at least 1 vote).

    Your second random number needs to be between 0/1 and (500-first random number). It can't be between 0 and 500 again, or you might go well beyond 500 votes.

    Your third number of votes is then equal to 500 - first random number - second random number. So it's not random at all. It's just a calculation.
    This method would be far more efficient at getting to a final answer, though it is further from a simulation of a voting process.
    Last edited by laserlight; 10-30-2012 at 01:26 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So it's an individual random voting program. Ah so!

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Well, it depends on what he wants... the "random" requirement could be satisfied by picking a winner, then distributing the votes as 168, 166, 166.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    Quote Originally Posted by laserlight View Post
    Well, it depends on what he wants... the "random" requirement could be satisfied by picking a winner, then distributing the votes as 168, 166, 166.
    Code:
    #include <stdio.h>              
    #include <time.h>
    #include <stdlib.h>
                    
    int main (void)         
    {
            int C_A = 0, C_B = 0, C_C = 0, C_D = 0;
            int loop = 0, i = 0, pre = 0;
            int votes [500]; 
            srandom( (unsigned) time(NULL) );
                                    
                    while( i == 1 && i == 2 && i == 3 && i == 4)
                    {
                            for(i = 0; i < 501; i++)
                            {
                                    votes [i] = random ( ) % 4 + 1;
                                    printf("\n %d \n", votes[0]);
                            }
             
                     if(i == 1)                {
                            C_A = C_A + 1;
                    }
                     
                     if(i == 2)
                    {
                            C_B = C_B + 1;
                    }
                    
                     if(i == 3)
                    {
                            C_C = C_C + 1;
                    }
                     
                     if(i == 4)
                    {
                            C_D = C_D + 1;
                    }
    
    
                            
                            loop = 0;
                    }
    
            printf("\n %d \n", C_A);
            printf("\n %d \n", C_B);
            printf("\n %d \n", C_C);
            printf("\n %d \n", C_D);
    
    
                     
            return (0);
    }
    So can anyone tell me what's wrong with this code. M keep getting 0 for every candidate ????????!!!!!
    Last edited by newbi; 10-30-2012 at 02:29 AM.

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    the "pre" things is actually precinct. I wanted 2 do it 5 times.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    What is the algorithm that you are trying to implement?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate numbers according to Normal Distribution
    By anirban in forum C Programming
    Replies: 1
    Last Post: 11-27-2010, 07:53 AM
  2. Ranom numbers having uniform distribution
    By edesign in forum C Programming
    Replies: 9
    Last Post: 08-16-2009, 05:56 AM
  3. z-distribution function in C
    By petermichaux in forum C Programming
    Replies: 3
    Last Post: 01-14-2004, 12:37 AM
  4. Which distribution of Linux should I get?
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 50
    Last Post: 01-19-2003, 08:26 AM
  5. Best Distribution
    By gnu-ehacks in forum Linux Programming
    Replies: 4
    Last Post: 11-21-2001, 02:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21