please help me--random printing

This is a discussion on please help me--random printing within the C Programming forums, part of the General Programming Boards category; Can somebody help me with the following logic-- how to do it I need to print a page once every ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    Question please help me--random printing

    Can somebody help me with the following logic-- how to do it

    I need to print a page once every 25 transactions but -- which transaction it prints for should be random.

    I mean it might print for 1 st transaction in the first 25 transactions and print for 35th transaction in the next 25 transactions(25-50)-- basically this should be random making sure that it is printing only once every 25 transactions starting from 1.

    please help me with this.....

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Something like this ?
    Code:
    struct transaction {
       ....
    };
    
    
    int main() {
       #define MAXTRANS 1000;
       transaction transactions[MAXTRANS];
       srand(time(0));
       for ( int i = 0; i < MAXTRANS / 25; ++i ) {
          int rnd = rand() % 25;
          printit( transactions[i*25 +rnd] );
       }     
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Thanks a lot for the prompt reply but I do not have MAXTRANS..maximum number of transactions and I have to print not the transaction but a same page for every random transaction selected

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Have a loop that just keeps repeating as long as you have transactions. In the loop:

    - Pick a random number between 1 and 25
    - Keep count of the number of transactions. If this number matches your random number, print a report.
    - Increment your count of the transactions. If it's now over 25, set it equal to 1 again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

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