every 10th number to be random

This is a discussion on every 10th number to be random within the C Programming forums, part of the General Programming Boards category; Hello All, I am tasked with creating an integer file from 0 - 100. I am to make every 10th ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    175

    every 10th number to be random

    Hello All,
    I am tasked with creating an integer file from 0 - 100. I am to make every 10th integer to be a random number.
    i.e.
    Code:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    25
    11
    12
    13
    14
    .
    .
    .
    I realize that is actually the 11th digit starting from 0. I have enclosed my code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    
    int i, ran;
    
    FILE *fp;
    fp = fopen("100_semi.txt", "w+");
    for (i = 0; i < 100; i++){
        if (i/10) {
            ran = rand() % 100;
            fprintf(fp, "%d\n", ran);
            //fprintf(fp, "%d\n", i);
        }
        else fprintf (fp, "%d\n", i);
    }
        //fprintf(fp, "%d\n", i);
    
    return 0;
    }
    My output as of right now is:

    Code:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    41
    67
    34
    0
    69
    24
    78
    58
    62
    64
    5
    I am hoping someone can tell me what I need to do to correct my problem.
    I truely appreciate it.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    I think this is what you are looking for
    Code:
    if ((i+1)%10 == 0) {
       /* random*/
    }
    I think we should use the module for this kind of operations
    your result was a position before so i just added one

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    175
    Quote Originally Posted by std10093 View Post
    I think this is what you are looking for
    Code:
    if ((i+1)%10 == 0) {
       /* random*/
    }
    I think we should use the module for this kind of operations
    your result was a position before so i just added one
    Thank for you help. That did it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  3. Replies: 2
    Last Post: 12-25-2003, 12:31 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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