mardi 3 janvier 2017

how to choice the rand () rang in c?

i want to generate a random number in array between 20 and 30 but it genrat more than 30 and this is my code ..

#include <stdio.h>
#include <stdlib.h>
int main (){

    
    int s[7];
    for (int i = 0 ;i<7 ; i++) {
        s[i]=20+(rand()%30); 
    }
    for (int i = 0 ; i<7 ; i++) {
        printf("s[%d]=%d\n",i,s[i]);
        
    }
}



Aucun commentaire:

Enregistrer un commentaire