Search
 
SCRIPT & CODE EXAMPLE
 

CPP

C++ srand()

#include <iostream>
#include <cstdlib>  //required for rand(), srand()
#include <ctime>    //required for time()
using namespace std;

int main() {
    srand(time(0));     //randomizing results... (using time as an input)
    
    const int totalNumbersGenerated = 30;
    const int minRange = 1, maxRange = 20;

    cout<<"
Printing "<<totalNumbersGenerated<<" random integer numbers (from "<<minRange<<" to "<<maxRange<<"):
";
    
    for(int i=1;i<=totalNumbersGenerated;i++){
        //generating random number in specified range (inclusive)
        cout<<1+((rand () % maxRange) + minRange - 1)<<" ";
    }
    
    cout<<endl;
    return 0;
}
Comment

srand() c++

#include <iostream>
#include <time.h> //you need to include this so you can use time

srand(unsigned int(time(NULL))); 
// this will try to "randomize" the value according to the current time
// some compilers will treat it as a warning if you dont define it as unsigned
Comment

PREVIOUS NEXT
Code Example
Cpp :: fiunction in c++ 
Cpp :: how to read and parse a json file with rapidjson 
Cpp :: cases in cpp 
Cpp :: appending int to string in cpp 
Cpp :: c++ replace string 
Cpp :: random number cpp 
Cpp :: c++ type casting 
Cpp :: c++ printf char as hex 
Cpp :: latex table landscape 
Cpp :: vector of strings initialization c++ 
Cpp :: c++ nagetive to positive numbers 
Cpp :: int to string c++ 
Cpp :: How to reverse a string in c++ using reverse function 
Cpp :: delete a node from binery search tree c++ 
Cpp :: sieve of eratosthenes algorithm in c++ 
Cpp :: How to pause a c++ program. 
Cpp :: c++ get ascii value of char 
Cpp :: print 2d array c++ 
Cpp :: how to send email in c++ program 
Cpp :: Quicksort taking random pivot 
Cpp :: c++ call by reference 
Cpp :: are strings mutable in c++ 
Cpp :: powershell get uptime remote computer 
Cpp :: filling 2d array with 0 c++ 
Cpp :: stoi() c++ 
Cpp :: union of two arrays leetcode 
Cpp :: length of string in c++ 
Cpp :: remove element from vector c++ 
Cpp :: notepad++ 
Cpp :: sort c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =