C में Random Numbers
In this page:
#include <stdlib.h>
#include <time.h>
srand(time(NULL)); // seed once at program start
int n = rand(); // 0 to RAND_MAX
int in_range = rand() % max + min;
rand() से Numbers Generate करना
rand(), stdlib.h में declared, हर call पर 0 और implementation-defined constant RAND_MAX के बीच कहीं एक pseudo-random integer return करता है, किसी genuinely unpredictable physical source की बजाय एक internal algorithm से numbers generate करते हुए।
उदाहरण: Generating Numbers with rand()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Declare n and set it to rand()
int n = rand();
// Print formatted text to the screen
printf("%d", n >= 0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
srand() से Seed Set करना
पहले srand() call किए बिना, rand() हमेशा exact same internal state से शुरू होता है और इसलिए program की हर single run पर identical sequence of numbers produce करता है, जो reproducible testing के लिए useful है लेकिन किसी user को random महसूस कराने वाली किसी भी चीज़ के लिए नहीं।
उदाहरण: Setting the Seed with srand()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
srand(1);
// Declare n and set it to rand()
int n = rand();
// Print formatted text to the screen
printf("%d", n >= 0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
time() से Randomize करना
srand(time(NULL)); के जरिए current system time से srand() seed करना हर program run को random numbers का एक अलग sequence produce कराने का standard तरीका है, क्योंकि program हर बार शुरू होने पर current time अलग (और effectively unpredictable) होता है।
उदाहरण: Randomizing with time()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
#include <time.h>
// Program execution starts in main()
int main() {
srand(time(NULL));
// Declare n and set it to rand()
int n = rand();
// Print formatted text to the screen
printf("%d", n >= 0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Random Ranges Limit करना
rand() के output पर modulo operator apply करना, जैसे rand() % 10, generated values को एक छोटी range तक restrict करता है (इस example में 0 से 9), जो dice rolls या array indices जैसी specific bounds के अंदर random numbers generate करने की usual technique है।
उदाहरण: Limiting Random Ranges
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
srand(1);
// Declare n and set it to rand() % 10
int n = rand() % 10;
// Print formatted text to the screen
printf("%d", n >= 0 && n < 10);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Random Floats Generate करना
rand() के integer result को floating-point constant RAND_MAX से divide करना 0.0 और 1.0 के बीच एक random decimal value produce करता है, जो random floats या percentages generate करने, या किसी दूसरी custom floating-point range में scale करने के लिए standard building block है।
उदाहरण: Generating Random Floats
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
srand(1);
// Declare f and set it to rand() / (double)RAND_MAX
double f = rand() / (double)RAND_MAX;
// Print formatted text to the screen
printf("%d", f >= 0.0 && f <= 1.0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
srand()से seed किए बिनाrand()call करना, ताकि हर run same numbers दे।- एक loop के अंदर
srand(time(NULL))call करना, जो हर बार same value से seed reset करता है। rand() % nइस्तेमाल करना और एक perfectly even distribution की उम्मीद करना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: