Function to generate a random number in a random range

Kate_12 0 Tallied Votes 270 Views Share

Can someone please give me tips on how to accomplish generating a random number within a random range. See 'roll_dice'

#include <iostream>
#include <limits>
#include <ctime>
#include <iomanip>
#include <cstdlib>
using namespace std;
using std::cout;
using std::cin;


struct Sanity {

    unsigned int turns = std::numeric_limits<int>::max();
    unsigned int sanity = std::numeric_limits<int>::max();
    int randNum = 0;
    int choice = 0;
    int low = 0;
    int high = 0;

};

void game_greeting();
int roll_dice(Sanity& s1);
int even_odd(Sanity& s1);

int main()
{
    Sanity s1;

    game_greeting();
    roll_dice(s1);
    even_odd(s1);


}

int roll_dice(Sanity &s1)
{
    srand(time(NULL));    
    s1.randNum = rand() % roll_dice(s1) + roll_dice(s1);
    return s1.randNum;
}
#include <iostream>
#include <limits>
#include <ctime>
#include <iomanip>
#include <cstdlib>
using namespace std;
using std::cout;
using std::cin;


struct Sanity {

    unsigned int turns = std::numeric_limits<int>::max();
    unsigned int sanity = std::numeric_limits<int>::max();
    int randNum = 0;
    int choice = 0;
    int low = 0;
    int high = 0;
    
};

void game_greeting();
int roll_dice(Sanity& s1);
int even_odd(Sanity& s1);

int main()
{
    Sanity s1;

    game_greeting();
    roll_dice(s1);
    even_odd(s1);

    
}

int roll_dice(Sanity &s1)
{
    srand(time(NULL));
    s1.randNum = rand() % roll_dice(s1) + roll_dice(s1);
    return s1.randNum;
}
Reverend Jim 4,968 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
rproffitt 2,662 "Nothing to see here." Moderator

I'm sure you can find the random function and more but it appears you want to scale and offset the result.
Given the return from the common random function is between 0 and 1 floating point you only need to take that result, multiple by the high minus the lower value and then add the lower value. In short, scale then offset.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I would recommend moving away from the older C-style srand()/rand() combination, and use the C++ <random> library, which is both more flexible and gives finer control. While it requires more effort to set up, it would be well worth that effort.

#include <iostream>
#include <limits>
#include <ctime>
#include <iomanip>
#include <random>


struct Sanity {

    unsigned int turns = std::numeric_limits<int>::max();
    unsigned int sanity = std::numeric_limits<int>::max();
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,6);
    int randNum = 0;
    int choice = 0;
    int low = 0;
    int high = 0;

};

void game_greeting();
int roll_dice(Sanity& s1);
int even_odd(Sanity& s1);

int main()
{
    Sanity s1;

    game_greeting();
    roll_dice(s1);
    even_odd(s1);


}

int roll_dice(Sanity &s1)
{
    s1.randNum = distribution(generator) + distribution(generator);

    return s1.randNum;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.