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.

40,000 questions

51,945 answers

573 users

How to generate 4 digits random numbers in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
 
int getRandom(int lower, int upper) { 
	return rand() % (upper - lower + 1) + lower; 
} 
 
int main()
{
	srand(time(0)); 
	 
	for (int i = 0; i < 10; i++)
		printf("%d\n", getRandom(1000, 9999));
      
    return 0;
}
 
 
 
/*
run:
 
6275
4675
6392
4198
2545
9283
5203
9172
3706
4913
   
*/

 



answered Jul 4, 2020 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
 
int main()
{
	srand(time(0)); 
	 
	for (int i = 0; i < 10; i++)
		printf("%d\n", 1000 + (rand() % 9000));
      
    return 0;
}
 
 
 
/*
run:
 
6637
3332
8467
6250
9583
4398
8237
2924
1485
1606
   
*/

 

 



answered Jul 4, 2020 by avibootz

Related questions

1 answer 142 views
1 answer 94 views
1 answer 115 views
1 answer 111 views
1 answer 97 views
1 answer 132 views
1 answer 143 views
...