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,895 questions

51,826 answers

573 users

How to pick 2 random digits from a number in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
 
int main(void) {
    int num = 213859;
    char str[6];
 
    sprintf(str,"%d", num);
 
    int size = strlen(str);
   
    srand((unsigned int)time(NULL));
   
    int digit2, digit1 = str[rand() % (size - 1)] - '0';
    
    do {
        digit2 = str[rand() % (size - 1)] - '0';
    } while (digit1 == digit2);
        
   
    printf("%d %d", digit1, digit2);
  
    return 0;
}
  
  
  
/*
run:
    
8 2
    
*/

 



answered Feb 3, 2024 by avibootz

Related questions

1 answer 129 views
1 answer 96 views
1 answer 148 views
1 answer 126 views
1 answer 145 views
1 answer 119 views
...