← Back to C Course | Chapter 14: Additional Topics | Lesson 8 of 9

C में Random Numbers

Random numbers unpredictable-दिखने वाली values हैं जो एक program बनाता है, dice roll करने जैसा। आप rand को एक starting seed देते हैं ताकि sequence हर बार अलग हो।
Syntax
c
#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()

c
// 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;
}

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()

c
// 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;
}

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()

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. srand() से seed किए बिना rand() call करना, ताकि हर run same numbers दे।
  2. एक loop के अंदर srand(time(NULL)) call करना, जो हर बार same value से seed reset करता है।
  3. rand() % n इस्तेमाल करना और एक perfectly even distribution की उम्मीद करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.