Answered by : justin-moua
//How to create random integers within a specific range in C langauge.
srand(time(0))
number = (rand() % (upper - lower + 1)) + lower
//upper = max number
//lower = least number
Source : https://www.includehelp.com/c-programs/generate-random-numbers-within-a-range.aspx#:~:text=The%20rand()%20function%20generates,random%20number%20between%20given%20ranges.&text=Explanation%3A,number%20between%201%20to%206. | Last Update : Tue, 28 Jul 20
Answered by : samy-chaabi
#include <stdio.h>
#include <stdlib.h>
#include <time.h> #define randnum(min, max) \ ((rand() % (int)(((max) + 1) - (min))) + (min))
int main()
{ srand(time(NULL)); printf("%d\n", randnum(1, 70));
}
Source : https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c | Last Update : Sat, 10 Sep 22
Answered by : davide-santoro
#include <stdio.h>
#include <time.h>
int main(){ /*this is the seed that is created based on how much time has passed since the start of unix time. In this way the seed will always vary every time the program is opened*/ srand(time(NULL)); int max; int min; int n; printf("give me the minimum number?\n"); scanf("%d", &min); printf("give me the maximum number?\n"); scanf("%d", &max); //method to derive a random number n = rand() % (max - min + 1) + min; printf("random number:%d", n); return 0;
}
Source : | Last Update : Sat, 09 Oct 21
Answered by : hilarious-hare-iwi2ar8bhum0
#include <time.h>
#include <stdlib.h>
srand(time(NULL)); // Initialization, should only be called once.
int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX.
Source : https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c | Last Update : Thu, 25 Jun 20
Answered by : lara-combina
//Note: Don't use rand() for security.
#include <time.h>
#include <stdlib.h>
srand(time(NULL)); // Initialization, should only be called once.
int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX.
Source : https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c | Last Update : Tue, 30 Aug 22