SlideShare a Scribd company logo
1 of 11
However, the numbers generated by the rand() are not
random because it generates the same sequence each time
the code executed. So, if we run the code again, we'll get the
same sequence repeated as in the previous run.
 To make a different sequence of numbers, we
should specify a seed as an argument to
a srand() function.
 Eg: srand(98765);
 To generate a ever changing sequence, we
need to feed something other than static
integer to the argument of
the srand() function.
 The best solution is to seed the rand() function
using the current time as the argument
to srand(), by calling time() function from the
standard C++ library, <time.h>.
 This returns the system time. Then, for
portability, we cast as an integer type:
eg : srand((int) time(0));
1st run
2nd
run
 Generating a random number with a specified
range:
The rand( ) function by default generates a
number in range 0 to RAND_MAX. Now if you
want to generate a number with our own
specified range say 5 to 10, then you can change
the rand( ) as below:
(rand( ) % U) + L
(rand( ) % 10) + 5
or
random( U-L +1) + L
random(10-5+1) + 5
 //Program to generate 5 random numbers in the range 5 - 14
//program is using randomize( ) and random( ) functions.
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
# include <time.h>
int main()
{
int num,i;
randomize();
for(i=0;i<5;++i)
{
num=random(14-5+1)+5 ; //num = random(U – L + 1) + L ;
cout<<num<<' ';
}
getch(); }
randomize ( ) function internally implements a macro that calls
time function of time.h. So, you need to use time.h header file in
your program to implement random ( ) function.

More Related Content

What's hot

Solidity Tutorials- Transer/Send ether- Transfer(), send(), call() function i...
Solidity Tutorials- Transer/Send ether- Transfer(), send(), call() function i...Solidity Tutorials- Transer/Send ether- Transfer(), send(), call() function i...
Solidity Tutorials- Transer/Send ether- Transfer(), send(), call() function i...Tutorials Diary
 
Rust Synchronization Primitives
Rust Synchronization PrimitivesRust Synchronization Primitives
Rust Synchronization PrimitivesCorey Richardson
 
Implementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HWImplementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HWJoe Jiang
 
Heroku's Ryan Smith at Waza 2013: Predictable Failure
Heroku's Ryan Smith at Waza 2013: Predictable FailureHeroku's Ryan Smith at Waza 2013: Predictable Failure
Heroku's Ryan Smith at Waza 2013: Predictable FailureHeroku
 
4.5 sec and csc worked 3rd
4.5   sec and csc worked 3rd4.5   sec and csc worked 3rd
4.5 sec and csc worked 3rdJonna Ramsey
 
PythonPythagoreanTheorem
PythonPythagoreanTheoremPythonPythagoreanTheorem
PythonPythagoreanTheoremMatt R
 
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)Sylvain Hallé
 
4.5 tan and cot.ppt worked
4.5   tan and cot.ppt worked4.5   tan and cot.ppt worked
4.5 tan and cot.ppt workedJonna Ramsey
 
Towers Hanoi Algorithm
Towers Hanoi AlgorithmTowers Hanoi Algorithm
Towers Hanoi AlgorithmJorge Jasso
 
Graphing day 1 worked
Graphing day 1 workedGraphing day 1 worked
Graphing day 1 workedJonna Ramsey
 
4.7 inverse functions.ppt worked
4.7   inverse functions.ppt worked4.7   inverse functions.ppt worked
4.7 inverse functions.ppt workedJonna Ramsey
 
Altitude San Francisco 2018: WebAssembly Tools & Applications
Altitude San Francisco 2018: WebAssembly Tools & ApplicationsAltitude San Francisco 2018: WebAssembly Tools & Applications
Altitude San Francisco 2018: WebAssembly Tools & ApplicationsFastly
 
Ruby Code Optimizations (for beginners)
Ruby Code Optimizations (for beginners)Ruby Code Optimizations (for beginners)
Ruby Code Optimizations (for beginners)chirantan.rajhans
 

What's hot (20)

Solidity Tutorials- Transer/Send ether- Transfer(), send(), call() function i...
Solidity Tutorials- Transer/Send ether- Transfer(), send(), call() function i...Solidity Tutorials- Transer/Send ether- Transfer(), send(), call() function i...
Solidity Tutorials- Transer/Send ether- Transfer(), send(), call() function i...
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Rust Synchronization Primitives
Rust Synchronization PrimitivesRust Synchronization Primitives
Rust Synchronization Primitives
 
Implementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HWImplementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HW
 
Heroku's Ryan Smith at Waza 2013: Predictable Failure
Heroku's Ryan Smith at Waza 2013: Predictable FailureHeroku's Ryan Smith at Waza 2013: Predictable Failure
Heroku's Ryan Smith at Waza 2013: Predictable Failure
 
4.5 sec and csc worked 3rd
4.5   sec and csc worked 3rd4.5   sec and csc worked 3rd
4.5 sec and csc worked 3rd
 
PythonPythagoreanTheorem
PythonPythagoreanTheoremPythonPythagoreanTheorem
PythonPythagoreanTheorem
 
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
 
4.5 tan and cot.ppt worked
4.5   tan and cot.ppt worked4.5   tan and cot.ppt worked
4.5 tan and cot.ppt worked
 
Towers Hanoi Algorithm
Towers Hanoi AlgorithmTowers Hanoi Algorithm
Towers Hanoi Algorithm
 
Number
NumberNumber
Number
 
Graphing day 1 worked
Graphing day 1 workedGraphing day 1 worked
Graphing day 1 worked
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
Complexity analysis
Complexity analysisComplexity analysis
Complexity analysis
 
4.7 inverse functions.ppt worked
4.7   inverse functions.ppt worked4.7   inverse functions.ppt worked
4.7 inverse functions.ppt worked
 
TypeScript
TypeScriptTypeScript
TypeScript
 
Altitude San Francisco 2018: WebAssembly Tools & Applications
Altitude San Francisco 2018: WebAssembly Tools & ApplicationsAltitude San Francisco 2018: WebAssembly Tools & Applications
Altitude San Francisco 2018: WebAssembly Tools & Applications
 
3. basics of python
3. basics of python3. basics of python
3. basics of python
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
Ruby Code Optimizations (for beginners)
Ruby Code Optimizations (for beginners)Ruby Code Optimizations (for beginners)
Ruby Code Optimizations (for beginners)
 

Similar to Random numbers c++ class 11 and 12

functions2-200924082810.pdf
functions2-200924082810.pdffunctions2-200924082810.pdf
functions2-200924082810.pdfpaijitk
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]vikram mahendra
 
Intro to tsql unit 10
Intro to tsql   unit 10Intro to tsql   unit 10
Intro to tsql unit 10Syed Asrarali
 
Problem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdfProblem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdfJUSTSTYLISH3B2MOHALI
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...AntareepMajumder
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfmallik3000
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsMorteza Mahdilar
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)Sylvain Hallé
 

Similar to Random numbers c++ class 11 and 12 (20)

functions2-200924082810.pdf
functions2-200924082810.pdffunctions2-200924082810.pdf
functions2-200924082810.pdf
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Intro to tsql unit 10
Intro to tsql   unit 10Intro to tsql   unit 10
Intro to tsql unit 10
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Problem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdfProblem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdf
 
Array &strings
Array &stringsArray &strings
Array &strings
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditions
 
C programming part4
C programming part4C programming part4
C programming part4
 
C programming part4
C programming part4C programming part4
C programming part4
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
Foxpro (1)
Foxpro (1)Foxpro (1)
Foxpro (1)
 
Unitii string
Unitii stringUnitii string
Unitii string
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
 

Recently uploaded

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

Random numbers c++ class 11 and 12

  • 1.
  • 2.
  • 3.
  • 4. However, the numbers generated by the rand() are not random because it generates the same sequence each time the code executed. So, if we run the code again, we'll get the same sequence repeated as in the previous run.
  • 5.  To make a different sequence of numbers, we should specify a seed as an argument to a srand() function.  Eg: srand(98765);  To generate a ever changing sequence, we need to feed something other than static integer to the argument of the srand() function.
  • 6.  The best solution is to seed the rand() function using the current time as the argument to srand(), by calling time() function from the standard C++ library, <time.h>.  This returns the system time. Then, for portability, we cast as an integer type: eg : srand((int) time(0));
  • 7.
  • 9.  Generating a random number with a specified range: The rand( ) function by default generates a number in range 0 to RAND_MAX. Now if you want to generate a number with our own specified range say 5 to 10, then you can change the rand( ) as below: (rand( ) % U) + L (rand( ) % 10) + 5 or random( U-L +1) + L random(10-5+1) + 5
  • 10.  //Program to generate 5 random numbers in the range 5 - 14 //program is using randomize( ) and random( ) functions. #include <iostream.h> #include <stdlib.h> #include <conio.h> # include <time.h> int main() { int num,i; randomize(); for(i=0;i<5;++i) { num=random(14-5+1)+5 ; //num = random(U – L + 1) + L ; cout<<num<<' '; } getch(); }
  • 11. randomize ( ) function internally implements a macro that calls time function of time.h. So, you need to use time.h header file in your program to implement random ( ) function.