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

51,793 answers

573 users

How to generate N random numbers between min and max into an array with C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
  
#define SIZE 7
   
int getRandom(const int min, const int max) {
    return (rand() % (max - min + 1)) + min; 
}
  
void GenerateNRandomNumbers(const int N, int array[], const int min, const int max) {
    for (int i = 0; i < N; i++) {
        array[i] = getRandom(min, max);
    }
}
  
int main(void) {
    srand(time(NULL));
    int array[SIZE];
    const int N = SIZE;
      
    GenerateNRandomNumbers(N, array, 1, 30);
      
    for (int i = 0; i < N; i++) {
        printf("%d ", array[i]);
    }
   
    return 0;
}
 
   
/*
run:
   
14 26 5 14 11 19 4 
  
*/

 



answered Sep 24, 2024 by avibootz
edited Sep 24, 2024 by avibootz

Related questions

1 answer 106 views
1 answer 105 views
1 answer 113 views
2 answers 111 views
...