random number in c++
C++
// C program to generate random numbers
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
// Driver program
int main(void)
{
// This program will create different sequence of
// random numbers on every program run
// Use current time as seed for random generator
srand(time(0));
for(int i = 0; i<5; i++)
printf(" %d ", rand());
return 0;
}
v1 = rand() % 100; // v1 in the range 0 to 99
v2 = rand() % 100 + 1; // v2 in the range 1 to 100
v3 = rand() % 30 + 1985; // v3 in the range 1985-2014 rand() % (max_number + 1 - minimum_number) + minimum_numberGuess the number (1 to 10): 5
The secret number is higher
Guess the number (1 to 10): 8
The secret number is lower
Guess the number (1 to 10): 7
Congratulations!
/* rand example: guess the number */
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int main ()
{
int iSecret, iGuess;
/* initialize random seed: */
srand (time(NULL));
/* generate secret number between 1 and 10: */
iSecret = rand() % 10 + 1;
do {
printf ("Guess the number (1 to 10): ");
scanf ("%d",&iGuess);
if (iSecret<iGuess) puts ("The secret number is lower");
else if (iSecret>iGuess) puts ("The secret number is higher");
} while (iSecret!=iGuess);
puts ("Congratulations!");
return 0;
}
Also in C++:
- Title
- c++ char print fixed
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- c++ delet from memory
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- lambda operator in c++
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- how to get the largest number in a c++ array
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- longest common subsequence
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- remove value from vector c++
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- min in vector c++
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- extends c++
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- find height of a tree
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- translate
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++
- Title
- sfml base program
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- UPARAM(ref)
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- int random string generator c++
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- *min_element in c++
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- vector pop back
- Category
- C++
- Title
- std::reverse
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- how to make a switch case statement in c++
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- how are graphics in games made
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- equal elements in two arrays in c++
- Category
- C++
- Title
- c++ remove element from vector
- Category
- C++
- Title
- static_cast c++
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- how to convert number to string
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- C++ while
- Category
- C++
- Title
- c++ evaluate expression
- Category
- C++