Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to generate random times random numbers that include specific digit x times in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 

void generate_numbers_that_include_specific_digit_x_times(int xtims, int digit, int rndtimes, int to) {
    srand(time(NULL));
     
    for (int i = 1; i <= rndtimes; i++) {
        int n = rand() % to + 1;
        int copy_n = n;
        int count = 0;
        
        while (n > 0) {
            if (n % 10 == digit)
                count++;
            n = n / 10;
        }
        
        if (count == xtims) {
            printf("%d\n", copy_n);
        }
    }
}

int main(void)
{
    generate_numbers_that_include_specific_digit_x_times(3, 7, 2000, 1000000);

    return 0;
}
 
 
 
/*
run:
 
7784767
272767
773837
147677
776547
716747
777063
778790
478717
967773
207767
779478
577378
272477
474727
677987
870737
7707
738477
947779
578707
747057
748077
247737
76727
907077
734177
775475
767713
777500
171771
77078
 
*/

 



answered Mar 7, 2024 by avibootz
edited Mar 7, 2024 by avibootz
...