random help

Hey everyone,

I'm writing a program for my assignment, and I wish to try out randomizing. I've one random test done a simple true false and its works well. My next question is. Would it be possible to have a set number of items that is inputted by the user and have each item randomly put into a weight category? Below is the code i've written, I have a little more to write out but that is easy stuff i can do on my own. its just the random question I would like help with.

int _tmain(int argc, _TCHAR* argv[])
{
// used variables

double total,total_xl, total_l, total_m, total_s, total_b, weight;
double XL,L,M,S,B;
int egg;

cout << "\n\n\t How many eggs was Brought in delivery?: ";
cin >> egg; // input for amount of eggs.

total = egg*0.02;
cout << "\n\n\t The basic entitlement for : " << egg << "eggs is: " << total << endl;

system("pause"); // pauses system before solmonella test

cout << "\n\n\t Solmonella test";

bool solmonellatester ();
{
srand((unsigned)time(0));
int i = rand() % 2;
if (i == 1)
{
cout << "\n\n\t Solmonella test returned positive, no payment destroy batch!";
}
else if (i == 0)
{
cout << "\n\n\t No solmonella detected in batch, please continue";
}
}
if (weight <53)
cout << "\n\n\t Grade A small eggs";
total= total_s*0.05;
else if (weight >=53)
cout << "\n\n\t Grade A Medium eggs";
total= total_m*0.06;
else if (weight >=63)
cout << "\n\n\t Grade A Large eggs";
total=total_l*0.07;
else if (weight >=73)
cout << "\n\n\t Grade A Xtra large Eggs";
total=total_xl*0.08;
Yes.

Note 1: call srand() at most once. The rand() you can call many times.

Note 2: You create 0 or 1 with rand(). You could create a number within larger range, say [40..86] and use that as "weight".

Note 3: Look at the <random>, the C++11 way to generate numbers. There are distributions, for example to make most eggs medium-size.

PS. _tmain() is an MS extension, not part of C++ standard.
Last edited on
hi keskiverto

thank you for the quick reply ill work on the advice you have given and let you know if i get it to work :-)

thank you
Topic archived. No new replies allowed.