#include <iostream>
#include <ctime>
using namespace std;
int randBetween()
{
unsigned seed = time(0);
srand(seed);
const int MIN_VALUE = -100;
const int MAX_VALUE = 100;
return (rand() % (MAX_VALUE - MIN_VALUE 1 )) MIN_VALUE;
}
int main() {
const int SIZE = 10;
int myArray[SIZE];
// ^^ how do I use function above to give myArray random values?
return 0;
}
I wanna use that rand function to give my array random values from -100 to 100 but I dont know how to put that rand function in the array so that my array can generate random number inside it hopefully that makes sense how do I do that?
CodePudding user response:
First we'll take a look at your code and critique it.
#include <iostream>
#include <ctime>
// MISSING <cstdlib> for C random functions
using namespace std; // Bad Practice
int randBetween()
{
unsigned seed = time(0); // Wrong placement; should only instantiate ONCE
srand(seed);
const int MIN_VALUE = -100;
const int MAX_VALUE = 100;
return (rand() % (MAX_VALUE - MIN_VALUE 1 )) MIN_VALUE;
// Modulo math tends to make the values in the lower end of the range more prevalent; i.e.,
// it's not very uniform.
}
int main() {
const int SIZE = 10; // All caps names for constants is not desirable; they can be confused for macros
int myArray[SIZE]; // Prefer std::array if the size is known, else std::vector for most cases
// ^^ how do I use function above to give myArray random values?
return 0;
}
The biggest issue is the use of C-style conventions when C provides better methods. In fact, you won't even need a function for this.
The secret sauce of getting the random numbers into your array is a loop. Make your loop visit every element and assign a new random number. Either directly, as in my first example, or by using a function as in my second example.
#include <array>
#include <iostream>
#include <random>
int main() {
const int minValue = -100;
const int maxValue = 100;
const int size = 10;
std::array<int, size> myArray;
// std::mt19937 is the goto PRNG in <random>
// This declaration also seeds the PRNG using std::random_device
// A std::uniform_int_distribution is exactly what it sounds like
// Every number in the range is equally likely to occur.
std::mt19937 prng(std::random_device{}());
std::uniform_int_distribution<int> dist(minValue, maxValue);
for (auto& i : myArray) {
i = dist(prng);
}
for (auto i : myArray) {
std::cout << i << ' ';
}
std::cout << '\n';
}
Now, if you want or need the function, there's a little bit of extra work that needs to be done.
#include <array>
#include <iostream>
#include <random>
int randBetween() {
const int minValue = -100;
const int maxValue = 100;
// The keyword static is required now so that the PRNG and distribution
// are not re-instantiated every time the function is called. This is
// important for them both to work as intended. Re-instantiating resets
// their state, and they constantly start from scratch. They must be allowed
// to persist their state for better results.
static std::mt19937 prng(std::random_device{}()); // Note the static
static std::uniform_int_distribution<int> dist(minValue, maxValue);
return dist(prng);
}
int main() {
const int size = 10;
std::array<int, size> myArray;
for (auto& i : myArray) {
i = randBetween();
}
for (auto i : myArray) {
std::cout << i << ' ';
}
std::cout << '\n';
}
Separating the PRNG from the distribution is good practice, especially when programs get larger. Then your single PRNG can feed multiple distributions if needed.
One output that I got:
-2 -37 81 85 -38 -62 31 -15 -12 -31
CodePudding user response:
Rolling up the comments into an answer:
You could use a loop:
for (size_t i = 0; i < SIZE; i)
{
myArray[i] = randBetween();
}
Or you could use the standard algorithm std::generate
:
std::generate(std::begin(myArray), std::end(myArray), randBetween);
This will loop over the array, calling randBetween
for each member and assigning it to the result.
You have a big issue, however. You should not call srand
each time you want a random number. Call it once, in the beginning of main
. As you have it now, you will likely fill the array with one number for a given run. There is a tiny chance the number will change part way through the array if the second of the current time changes during the program run.
Also the C Standard Library for random numbers is much preferable to rand
. And I'd recommend using a standard container rather than a native array.
CodePudding user response:
An alternate way would be to use PRNG(Pseudo Random Number Generator). This is from the include random, include functional header. Like this, run this in C 17 compiler:
#include <iostream>
#include <random>
#include <functional> // For std::bind()
auto createUniformPseudoRandomNumberGenerator(double max)
{
std::random_device seeder;
std::default_random_engine generator{seeder()};
std::uniform_real_distribution distribution{0.0, max};
return std::bind(distribution, generator);
}
int main()
{
const int SIZE = 10;
int myArray[SIZE];
double limit{};
std::cout<<"Enter max number limit.\n";
std::cin>>limit;
static auto random_number=createUniformPseudoRandomNumberGenerator(limit);
for (size_t i{};i<SIZE; i)
{
myArray[i]= random_number();
}
return 0;
}