Answers for "random function c++"

2

c++ how to generate a random number in a range

min + ( std::rand() % ( max - min + 1 ) )
Posted by: Guest on August-22-2020
3

random in c++

#include <iostream>
#include <stdlib.h>     
#include <time.h> 
using namespace std;

int main()
{
	int num;
	srand(time(0));
		num = rand() % 10 + 1;
		cout << num << endl;
}
Posted by: Guest on May-09-2021
7

c++ random

#include <cstdlib>
#include <iostream>
#include <ctime>
 
int main() 
{
    std::srand(std::time(nullptr)); // use current time as seed for random generator
    int random_variable = std::rand();
    std::cout << "Random value on [0 " << RAND_MAX << "]: " 
              << random_variable << '\n';
}
Posted by: Guest on May-16-2020
2

random number cpp

// Add thus to with the headers
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// Generate a function that will give values between l and r inclusive
auto dist = uniform_int_distribution<int>(l, r);
// get the random number using dist(rng);
Posted by: Guest on April-21-2021
1

random number generator c++

/* 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;
}
Posted by: Guest on December-23-2019
-1

random function c++

#include<iostream>
#include<cstdlib>
using namespace std;
 
int main(){
 
    // Providing a seed value
    srand((unsigned) time(NULL));
 
    // Loop to get 5 random numbers
    for(int i=1; i<=5; i++){
         
        // Retrieve a random number between 100 and 200
        // Offset = 100
        // Range = 101
        int random = 100 + (rand() % 101);
 
        // Print the random number
        cout<<random<<endl;
    }
 
    return 1;
}
Posted by: Guest on June-14-2021

Browse Popular Code Answers by Language