samedi 17 février 2018

Random float within a range?

I am trying to create a random number between 15.00 and 40.00, for use in a calculation. The specific task is to build a program that takes in the number of tickets sold and calculate the total income from these ticket sales, with three of the ticket types having a constant price value, and one of the ticket types having a random price value between $15.00 and $40.00. How can I do this?

I've come close with this, but only if I declare the mystmax and mystmin as 'int'. I would like them to be double, so I can have every decimal value between $15.00 and $40.00:

  unsigned seed = time(0);

    srand(seed);
    double mystery = (rand() % (mystmax - mystmin+1))+mystmin;

Here is the error I get when I try to have the min and max as doubles:

"Invalid operands of the types 'int' and 'double' to binary 'operator%'"

What can I do to remedy this?




Aucun commentaire:

Enregistrer un commentaire