Unexpected value using random number generator as a function in C++ - c++

C++ beginner here with a problem using a functions. Please help!
I am trying to make a random number generator function that I can call from within int main to cout a randomly generated float between 0.0 and 1.0 to my screen. When I create the generator in int main and cout it runs fine and returns a float just the way I want, but when I put the same code in a function and call it in int main I get some mix of letters and numbers that makes no sense.
I understand that I could just use the code in the way it works and ignore it but at this point I really want to know why it's not working the way I'm trying to make it work. I've looked all over for the answer and feel as if I'm missing some really basic knowledge about the way functions work in C++.
Why is the code that works perfectly fine in int main returning gobbledygook when called from a function?
Random number generator in function (returns nonsense):
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
float roll();
int main(){
cout<<roll<<endl;
system("PAUSE");
return 0;
}
float roll(){
default_random_engine generator;
uniform_real_distribution<float> distribution(0.0f,1.0f);
return distribution(generator);
}
Random number generator in int main (this one returns a float):
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main()
{
default_random_engine generator;
uniform_real_distribution<float> distribution(0.0f,1.0f);
float roll = distribution(generator);
cout<<roll<<endl;
system("PAUSE");
return 0;
}

This is wrong:
cout<<roll<<endl;
You want:
cout<<roll()<<endl;
You want to call the function.

You just put the name of function which means just the address of it. Call it by using as defined.
For more information please check the tutorial.
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
float roll();
int main(){
cout<<roll();<<endl;
system("PAUSE");
return 0;
}
float roll(){
default_random_engine generator;
uniform_real_distribution<float> distribution(0.0f,1.0f);
return distribution(generator);
}

Related

rand() function generates the next number by ascending to the previous number

Please check code given below. The random number it generates for each execution is the increment of the previous generated number in the previous execution.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
cout<<"\n Random Number : "<<rand()<<endl;
cin.get();
return 1;
}
Please executeit for 5-6 times and you will see that the random numbers are increasing for each execution and they are very close to each other.
Note : Please use CodeBlocks or Visual studio to check it, not the online compilers.
Actually I found a way to solve my problem but still it might not be an answer to my question.
Anyway the problem is not about srand() or rand() functions but it is about the function time(NULL). Since I am trying to run this code on Windows, instead of using time(NULL) as a parameter for srand(), I used GetTickCount() and now it generates random numbers properly for each execution.
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
int main()
{
srand(GetTickCount());
cout<<"\n Random Number : "<<rand();
cout<<"\n";
cin.get();
return 1;
}

return an array from an int method in c++

i am writing a code for a multidimensional array with two functions.
First function(read()) gets the value of each array, and the second one shows each of them.
My problem is return the gotten array from the read function.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include<cassert>
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
using namespace std;
typedef int Sec[2][2];
int read(Sec sec){
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
cin>>sec[i][j];
}
}
return sec;
}
void sho(){
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
cout<<sec[i][j];
}
}
}
int main() {
read(Sec sec);
sho(sec);
}
Here are your mistakes:
You don't need to return anything from read function because argument passed to this function is passed as pointer. Therefore, content on these addresses will be updated based on user input. This is perfectly fine function signature void read(Sec sec);
In your main function, you need first to initialize your local variable Sec sec; and then pass it to read function like this read(sec);
Hope this will help you!
try it this way:
#include <iostream>
//you dont need ctime here
#include <ctime>
//you dont need csdtlib here
#include <cstdlib>
//you dont need cmath here
#include <cmath>
//you dont need time.h here
#include <time.h>
//you dont need cassert here
#include<cassert>
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
using namespace std;
typedef int Sec[2][2];
//by adding the "&" symbol you give the function read a refrenze to a variable of
//type sec, which allows to change the values, it is like a derefrenzed pointer
void read(Sec& sec){
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
cin>>sec[i][j];
}
}
}
//you dont need a refrenze here, because u just want to read from the Sec object, if
//you want to run a bit faster you couldnt use:
//void show(const Sec& sec),
//this would give the function show a refrenze you cant edit, so perfectly for
//reading values
void show(Sec sec){
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
cout<<sec[i][j];
}
}
}
int main() {
Sec sec;
read(sec);
show(sec)
}

Generating very large Random numbers using Boost library

I am using boost library for generating very very large Random numbers in the range [0-2^32-1]. But boost library is not working well with this range. It just displaying an error message "Assertion failed min_arg<=max_arg"
Kindly help me out ! here is the code:
#include <iostream>
#include <ctime>
#include <cmath>
#include "boost/random.hpp"
#include <stdint.h>
using namespace std;
int main()
{
long double rangeMin = 0;
long long int rangeMax = (pow(2.0,32.0)-1);
typedef boost::uniform_int<> NumberDistribution;
typedef boost::mt19937 RandomNumberGenerator;
typedef boost::variate_generator<RandomNumberGenerator&,
NumberDistribution> Generator;
NumberDistribution distribution(rangeMin, rangeMax);
RandomNumberGenerator generator;
Generator numberGenerator(generator, distribution);
generator.seed(time(0)); // seed with the current time
cout << numberGenerator() <<endl;
return 0;
}
As per documentation, the default value of the template parameter of boost::uniform_int is int. If you want to generate long long int's, you'll need to use boost::uniform_int<long long int>.

efficient generation of numbers with Boost

I wish to generate more than 10^8 random numbers with Boost. They must be normally distributed with standard deviation 1 and mean 0. Here is my MWE:
#include <iostream>
#include <vector>
#include <time.h>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
using namespace std;
int main()
{
typedef boost::mt19937 ENG;
typedef boost::normal_distribution<double> DIST;
typedef boost::variate_generator<ENG,DIST> GEN;
ENG eng;
DIST dist(0,1);
GEN gen(eng,dist);
gen.engine().seed(time(0));
vector<double> nums;
for(int i=0; i<500; i++)
{
nums.push_back(gen());
}
return 0;
}
I have 2 questions in this regard:
Is the approach I am using to seed the engine correct? Or do I need to seed it before each number?
Is my method efficient? Or is there a better way?
EDIT Note that there is no bottleneck in the code as such. I am just wondering if my approach is correct from a professional point of view
I should say that the numbers (all of them) have to be scaled by a proper constant afterwards. My plan is to use a for-loop for this.
Best,
Niles.

Random number generation and encapsulation

I'm trying to generate random numbers with boost, and I'm having trouble getting it to work in a way that keeps everything encapsulated. I've got this piece of code in main(), which allows me to use roll() to generate a random number from [0,1)
boost::mt19937 rng(seed);
boost::uniform_01<> uniform_p;
boost::variate_generator< boost::mt19937, boost::uniform_01<> >
roll(rng, uniform_p);
Now I want to stick this in its own header/cpp file, with one function to obtain a seed and create roll, and others that call roll. But nothing can seem to see roll, and I'm pretty sure it will go out of scope after the function that calls it goes out of scope. I've tried playing around with static and extern, I've tried making roll part of a class so it won't go out of scope, but nothing seems to work.
How about having seed and rool functions declared in .h file, and their implementation in .cpp file. You could use static scoped_ptr to store helper objects i .cpp module and initialize them after static initialization phase. Thread-safety is another thing to keep in mind in this solution.
The following is a Matlab-like interface which I like to use for the generation of random numbers. You have to call seed() before using rand(). As Greg pointed out, scoped_ptr is handy to store the static variables (the Mersenne twister and the variate generator) which are needed. For thread-safety, protect these ressources with mutexes.
random.hpp
#ifndef RANDOM_HPP
#define RANDOM_HPP
void seed(unsigned s);
double rand();
#endif // RANDOM_HPP
random.cpp
#include "random.hpp"
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_01.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/scoped_ptr.hpp>
static boost::scoped_ptr<boost::mt19937> twister;
static boost::scoped_ptr<boost::variate_generator<boost::mt19937&,
boost::uniform_01<> > > vargen;
void seed(unsigned s)
{
twister.reset(new boost::mt19937(s));
vargen.reset(new boost::variate_generator<boost::mt19937&,
boost::uniform_01<> >(
*twister, boost::uniform_01<>()));
}
double rand()
{
assert(vargen.get() != 0);
return (*vargen)();
}
main.cpp
#include "random.hpp"
#include <iostream>
int main()
{
seed(42);
for (int i = 0; i < 10; ++i)
std::cout << rand() << std::endl;
return 0;
}