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 :: min heap in c++ 
Cpp :: write variable to file cpp 
Cpp :: 2d vector initialization in cpp 
Cpp :: C++ Converting Kelvin to Fahrenheit 
Cpp :: c++ std::find with lambda 
Cpp :: how to writt array in c++ 
Cpp :: qt qstring to double 
Cpp :: copy substring to another string c++ 
Cpp :: access first value in a set c++ 
Cpp :: C++ Area of a Rectangle 
Cpp :: C++ passing function arguments to a thread 
Cpp :: using find in vector c++ 
Cpp :: c++ check if string contains non alphanumeric 
Cpp :: how to hide the c++ console 
Cpp :: extends c++ 
Cpp :: time delay in c++ 
Cpp :: getch c++ library 
Cpp :: c++ memory leak 
Cpp :: pow in c++ 
Cpp :: string reversal 
Cpp :: random number in a range c++ 
Cpp :: lerp function c++ 
Cpp :: c++ printf char as hex 
Cpp :: slice std::array cpp 
Cpp :: c++ get full line of input 
Cpp :: remove last index of the string in c++ 
Cpp :: How to pause a c++ program. 
Cpp :: arduino funktion 
Cpp :: iterate vector in reverse c++ 
Cpp :: std distance 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =