I am creating a random number generator for numbers between 110,000 and 320,000. When I run it, no numbers are above 150,000. Is there some way to make sure that numbers above 150,000 are generated? Even generating thousands of numbers do not work. I am aware I have lots of things included. Here is the code:
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <conio.h>
#include <ctime>
#include <random>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
srand((unsigned) time(0));
int randomNumber;
for (int index = 0; index < 500; index++) {
randomNumber = (rand() % 320000) + 110000;
cout << randomNumber << endl;
}
}
As noted by John. You could use more recent random number generators easier to manipulate.
Adapting the code from C++ Reference about uniform_int_distribution
for your use case is straightforward:
#include <iostream>
#include <random>
int main(void) {
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<> distrib(110000, 320000);
for (int n=0; n<10; ++n)
// Use `distrib` to transform the random unsigned int generated by
// gen into an int in [110000, 320000]
std::cout << distrib(gen) << ' ';
std::cout << '\n';
}
Related
I am a new C++ programmer, I would like to ask you a simple question but I do not understand why output still give me repeating data:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream> // std::ifstream
#include <string.h>
#include <iostream>
#include <complex>
#include <sstream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
int nfft;
nfft = 256;
vector<float>f;
for(unsigned i=0; i<nfft; i++){
f.push_back(((i/(nfft-1))-0.5)*8e6);
}
for(unsigned i=0;i<nfft;i++){
cout << f[i] << endl;
}
}
My expectation is a vector: [-4.0000 -3.9686 -3.9373 -3.9059 -3.8745 -3.8431 -3.8118 -3.7804 -3.7490 -3.7176 -3.6863 -3.6549 ....... 3.9059 3.9373 3.9686 4.0000]
Please give me a hand to solve this problem.
Thank you so much.
(i/(nfft-1)) is well protected with parentheses and thus performs an integer division.
Since nfft > i you get zero => always the same value.
Example of how to ix:
f.push_back(((float(i)/(nfft-1))-0.5)*8e6)
For some reason I keep getting 6 every time. I know of another way to do a random dice roll, but I wanted to learn how to use the deafult_random_engine.
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int main()
{
default_random_engine randomGenerator(time(0));
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
}
But this bit of code works with the time(0).
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
// dice roll
{
srand(time(0));
for(int x = 1; x < 2; x++){
cout << 1+(rand()%6) << endl;
}
return 0;
}
It's almost certainly the use of time(0) as the culprit here.
You should probably opt for a method like this:
#include <iostream>
#include <string>
#include <chrono>
#include <random>
#include <ctime>
using namespace std;
int main() {
default_random_engine randomGenerator(std::random_device{}());
// OR:
// default_random_engine randomGenerator(
// (unsigned) chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
return 0;
}
While your original code always produced 6 on my system, this one seems a little more "adventurous":
pax> for i in {1..10}; do ./qq ; sleep 1 ; done
You rolled a 5
You rolled a 5
You rolled a 6
You rolled a 1
You rolled a 6
You rolled a 5
You rolled a 2
You rolled a 3
You rolled a 5
You rolled a 4
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int main()
{
mt19937 randomGenerator(time(0));
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
}
so my question in a nutshell would be: How do I access a random engine initialized in main() from another function without passing the engine as an argument?
I read that is a good habit to initialize a random engine only once in a program.
I would do it like this at the beginning of my main() function:
#include <iostream>
#include <random>
#include <ctime>
using std::cout;
using std::endl;
using std::default_random_engine;
using std::uniform_int_distribution;
int main() {
int seed = time(0);
default_random_engine engine(seed);
uniform_int_distribution<int> dist(0,100);
cout << "Random num: " << engine(dist) << endl;
}
But how would I make it accessible to other functions? E.g., in a nested structure where function func_a calls func_b, which is supposed to do something with a random number generated by the engine.
For example:
#include <iostream>
#include <random>
#include <ctime>
using std::cout;
using std::endl;
using std::default_random_engine;
using std::uniform_int_distribution;
void func_b() {
// do sth
cout << "Print a random number: " << endl;
}
void func_a() {
func_b();
}
int main() {
int seed = time(0);
default_random_engine engine(seed);
uniform_int_distribution<int> dist(0,100);
func_a();
}
EDIT: Problem Solved, thanks.
#include <iostream>
#include <random>
#include <ctime>
using std::cout;
using std::endl;
using std::default_random_engine;
using std::uniform_int_distribution;
////////////////////////
// GLOBAL VARIABLES
////////////////////////
int seed = time(0);
default_random_engine engine(seed);
////////////////////////
// FUNCTION DEFINITIONS
////////////////////////
void func_b() {
uniform_int_distribution<int> dist(0,100);
cout << dist(engine) << endl;
}
void func_a() {
func_b();
}
int main() {
int seed = time(0);
default_random_engine engine(seed);
uniform_int_distribution<int> dist(0,100);
func_a();
}
Thanks for all your help. I copy the solution to the answer section so it can be marked as accepted.
#include <iostream>
#include <random>
#include <ctime>
using std::cout;
using std::endl;
using std::default_random_engine;
using std::uniform_int_distribution;
////////////////////////
// GLOBAL VARIABLES
////////////////////////
int seed = time(0);
default_random_engine engine(seed);
////////////////////////
// FUNCTION DEFINITIONS
////////////////////////
void func_b() {
uniform_int_distribution<int> dist(0,100);
cout << dist(engine) << endl;
}
void func_a() {
func_b();
}
int main() {
int seed = time(0);
default_random_engine engine(seed);
uniform_int_distribution<int> dist(0,100);
func_a();
}
This question already has answers here:
c++ integer->std::string conversion. Simple function?
(3 answers)
Closed 9 years ago.
My code:
#include <iostream>
#include <random>
void main()
{
std::random_device rd;
std::cout << "Random value: " << rd() << std::endl;
system("pause");
}
How do i get the result rd(), and convert it to std::string?
Since you are asking how to convert the result of std::random_device to a string, and std::random_device returns an unsigned int. C++11 provides std::to_string, can be used to convert numbers to strings. See here.
#include <iostream>
#include <random>
#include <string>
int main()
{
std::random_device rd;
std::string str = std::to_string(rd());
std::cout << str << std::endl;
return 0;
}
Here's an example I found on http://en.academic.ru/dic.nsf/enwiki/11574016
#include <random>
#include <functional>
std::uniform_int_distribution<int> distribution(0, 99);
std::mt19937 engine; // Mersenne twister MT19937
auto generator = std::bind(distribution, engine);
int random = generator(); // Generate a uniform integral variate between 0 and 99.
int random2 = distribution(engine); // Generate another sample directly using the distribution and the engine objects.
I haven't worked with it before, but this might help you get started.
std::stringstream is one way to convert a number to a string, the code below shows various engines and distributions possible. It defaults to Mersenne Twister for the engine and the normal distribution. This is good reference for the options available:
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <sstream>
int main()
{
std::random_device rd;
//
// Engines
//
std::mt19937 e2(rd());
//std::knuth_b e2(rd());
//std::default_random_engine e2(rd()) ;
//
// Distribtuions
//
std::normal_distribution<> dist(2, 2);
//std::student_t_distribution<> dist(5);
//std::poisson_distribution<> dist(2);
//std::extreme_value_distribution<> dist(0,2);
std::stringstream s1 ;
s1 << dist(e2) ;
std::string str1 = s1.str();
std::cout << str1 << std::endl ;
}
another method to convert to a string would be to use std::to_string:
str1 = std::to_string( dist(e2) ) ;
#include <stdlib.h>
#include <time.h>
int main(){
srand(time(NULL));
unsigned int maxValue = 50;
std::cout << "Random value: " << rand()%maxValue; //random between 0-50
return 0;
}
I want to generate a random number for the temperature. The code I used is below:
int Temp()
{
// genreate random temperture
// initialize random seed:
srand ( time (NULL) );
// generate number between 1 and 100:
int t = rand() % 100 + 1;
std::cout << t << std::endl;
return t;
}
When the program is run, instead of displaying a number between 1 and 100, it display the following:
010C1109
Could someone explain where or why it is going wrong?
Edit: If anyone wondering I used the following:
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <map>
#include <cstdlib>
#include <cstring>
#pragma once
How to choose the way your numbers are displayed:
std::cout << std::hex << t << std::endl; //displays in hexadecimal
std::cout << std::dec << t << std::endl; //displays in decimal
In my example I see
58 in hexadecimal and 88 in decimal (5*16+8).
Here are official links for making the post complete.
C++ forum:
http://www.cplusplus.com/forum/windows/51591/
Details explained:
http://www.cplusplus.com/reference/ios/dec/