C++ Randomly generate Multiples of 16 - c++

Is it possible to generate random multiples of a specific number in C++? Let's say I want to generate multiples of 16 and only 16, how would this be done?

Randomly generate a number and multiply it with 16.

You can use the <random> header (C++11) to generate random numbers:
int get_random()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 20); // range 1 - 20
return dis(gen);
}
And multiply with 16:
int main()
{
std::vector<int> nums;
for (int i{}; i != 5; ++i)
nums.emplace_back(get_random() * 16);
}

Related

Using std::random_device can I set a range of floating point numbers but not include certain numbers?

As mentioned in the title I want to generate a random floating-point number between -10 and 10 but I want to make it so that it can't generate a number between -1.99 and 1.99.
My code for randomly generating numbers:
std::random_device random;
std::mt19937 gen(random());
std::uniform_real_distribution<float> dis(-10.0f, 10.0f);
for (int n = 0; n < 10; ++n)
{
std::cout << dis(gen); << std::endl;
}
you can use std::piecewise_constant_distribution:
#include <iostream>
#include <random>
int main() {
std::random_device rd;
std::mt19937 gen(rd());
// 50% of the time, generate a random number between -10.0f and -1.99f
// 50% of the time, generate a random number between 1.99f and 10.0f
std::vector<float> i{-10.0f, -1.99f, 1.99, 10.0f};
std::vector<float> w{1, 0, 1};
std::piecewise_constant_distribution<float> dis(i.begin(), i.end(), w.begin());
for (int n = 0; n < 10; ++n)
std::cout << dis(gen) << std::endl;
}
Demo.

How to generate 4 different random numbers in C++

I am making Bulls and Cows assignment from Bjarne Stroustrup's "Programming Principles and Practice Using C++" book (p. 130, exercise 13) and I want program to generate four different integers in the range 0 to 9 (e.g., 1234 but not 1122)
I made a vector to store numbers and a function which generates 4 numbers and adds them to vector, but numbers might be the same and I can't return numbers to main function
#include "../..//..//std_lib_facilities.h"
vector<int> gen4Nums(vector<int> secNum)
{
random_device rd; // obtain a random number from hardware
mt19937 eng(rd()); // seed the generator
uniform_int_distribution<> distr(0, 9); // define the range
secNum.clear();
for (int i = 0; i < 4; i++)
{
secNum.push_back(distr(eng));
cout << secNum[i];
}
return secNum;
}
int main()
{
vector<int> secNum;
gen4Nums(secNum);
}
I expect to return 4 different random numbers to the main function
You can ensure to get different random numbers in the result if you change your code like this:
#include <vector>
#include <random>
#include <algorithm>
using namespace std;
vector<int> gen4Nums()
{
vector<int> result;
random_device rd; // obtain a random number from hardware
mt19937 eng(rd()); // seed the generator
uniform_int_distribution<> distr(0, 9); // define the range
int i = 0;
while(i < 4) { // loop until you have collected the sufficient number of results
int randVal = distr(eng);
if(std::find(std::begin(result),std::end(result),randVal) == std::end(result)) {
// ^^^^^^^^^^^^ The above part is essential, only add random numbers to the result
// which aren't yet contained.
result.push_back(randVal);
cout << result[i];
++i;
}
}
return result;
}
int main() {
vector<int> secNum = gen4Nums();
}
Seems like you're trying to generate 4 unique random integers in the range 0...9.
You can do this by generating a vector of integers containing the values 0...9. Then shuffle the vector, as you want it to be a random selection of the integers. Lastly trimming the vector to the desired size, as you only want 4 unique random integers:
#include <vector>
#include <random>
#include <algorithm>
#include <numeric>
void gen4Nums(std::vector<int>& v) {
//Generate initial vector with values 0...9:
v.resize(10, 0);
std::iota(v.begin(), v.end(), 0);
//Shuffle the vector:
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(v.begin(), v.end(), g);
//Trim the vector to contain only 4 integers:
v.resize(4);
}

How can I generate a random number after every millisecond without closing the program in c++?

I want to generate a new random every time in a while loop until the loop breaks when it meets the condition in the while loop like 10 random numbers.
you could find something on https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
int main()
{
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis(1, 6);
for (int n=0; n<10; ++n)
//Use dis to transform the random unsigned int generated by gen into an int in [1, 6]
std::cout << dis(gen) << ' ';
std::cout << '\n';
}
where the for loop can obviously be replaced by a while

Generate random number in range without repetition

I am using C++ to generate n uniform numbers in a given range without repeatition. I want to save it in array(not vector). I found a code but it does not allow to generate number without repetition.
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(0,10-1); // guaranteed unbiased
auto random_integer = uni(rng);
For example, I will generate 5 random numbers in range 0-9 such as
1 0 3 8 6
This is my code
typedef unsigned int U32, *PU32;
U32 total_num = 5;
U32 *rndArray = new U32[total_num];
for (U32 i = 0; i < total_num; i++)
{
std::random_device rd // only used once to initialise (seed) engine
std::mt19937 rng(rd());
std::uniform_int_distribution<int> uni(0,10-1);
auto random_integer = uni(rng);
rndArray[i] = random_integer ;
}
Second way, I used the code bellow which allows without repetition. But it is not support in g++ (I am using g++ in ubuntu)
#include <random>
#include <algorithm>
#include <iterator>
#include <iostream>
typedef unsigned int U32;
int main()
{
U32 total_num = 5;
U32 *rndArray = new U32[total_num];
std::random_device rd;
std::mt19937 g(rd());
std::vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::shuffle(v.begin(), v.end(), g);
for (int i=0;i<5;i++)
{
rndArray[i]=v[i];
std::cout<< rndArray[i] << " ";
}
std::cout << "\n";
}
There's a couple ways you can do this.
If the random number is already in the array, then generate another one until you find a number that has not been seen before. This is quick to implement but has the disadvantage of very high running time theoretically.
Create the entire range to begin with in an array and then scramble it. To get k numbers, obtain the first k elements of the scrambled array.
Use the Fisher-Yates shuffle algorithm to shuffle a vector/array filled with all the numbers in the desired range: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
The easiest way to achieve what you want is to do what user Untitled123 suggested (see below). To compile: g++ -std=c++11 file.cpp
#include <vector>
#include <algorithm>
using namespace std;
int randomize(const int &i) return rand() % i;
int main() {
srand(unsigned(time(0)));
int n = 10;
vector<int> sample(n);
// generate numbers 0 .. n-1
iota(sample.begin(), sample.end(), 0);
// shuffle elements
random_shuffle(sample.begin(), sample.end(), randomize);
// grab the first five elements after shuffling
vector<int> chosen(sample.begin(), sample.begin() + 5);
// do stuff
return 0;
}

generating random numbers in c++

I need to generate a random number between 1 and n where n is unsigned int.
If n were int I would simply write 1 + rand()% n. But unfortunately n is unsigned int. What do you suggest?
rand() should be avoided whenever possible*.
Use http://en.cppreference.com/w/cpp/numeric/random
#include <random>
#include <iostream>
int main()
{
std::random_device rd;
std::mt19937 engine(rd());
std::uniform_int_distribution<unsigned> dist(1, 77);
for (int i = 0; i != 5; ++i)
std::cout << dist(engine) << '\n';
}
* Because it shares a global state, gets often implemented as a linear congruential engine which has a few drawbacks and it's range is often only 0-2^16. Also, % n where n is not an exact multiple of the range does not produce an uniform distribution.
Edit: This might seem like overkill, but technically one would want something like this, since mt19937 needs a bit of "warm up":
std::mt19937 create_seeded_rng()
{
std::random_device rd;
std::array<std::mt19937::result_type, std::mt19937::state_size> seed_data;
std::generate(seed_data.begin(), seed_data.end(), std::ref(rd));
std::seed_seq seq(seed_data.begin(), seed_data.end());
return std::mt19937(seq);
}
int main()
{
std::mt19937 rng = create_seeded_rng();
std::uniform_int_distribution<int> dist(0, 100);
for (unsigned i = 0; i != 100; ++i)
std::cout << dist(rng) << '\n';
}