13 Comments

double result = random (min,max)

and then convert result cu CString.

Also, for std::rand() don't i have to include something ?

#include <stdlib.h>

#include <time.h>

class Random {

public:

Random() {srand( (unsigned)time( NULL ) );}

double random(double min, double max) const {

double dRand = (double) rand () / (double) RAND_MAX;

double dResult = dRand * (dMax - dMin) + dMin;

return dResult;

}

};

//...

Random r;

double result = r.random (min,max);

You could have that also be

double random(double min, double max) const {

double dRand = (double) rand () / (double) RAND_MAX;

double dResult = dRand * (dMax - dMin) + dMin;

return dResult;

}

//...

double result = random (min,max);

without a class, but you'd have to be sure to seed the random number generator 'manually'.

this function can use min and max.

The standard function from the my Borland compiler has only an maximum to use.

Do you want also random values like 3042.492 or so?

//------------------------

#include <vcl.h>

#include <iostream.h>

#include <stdlib.h>

#include <conio.h>

#pragma hdrstop

double rand(int Min,int Max);

//------------------------

double rand(int Min,int Max) {

int Result;

Result=random(Max);

while(Result <=Min){

Result=random(Max);

}

return Result;

}

#pragma argsused

int main(int argc, char* argv[])

{

double RandomNumber,Min, Max;

cout <<"Random Numbers!:";

cout <<"\nMin";cin >>Min;

cout <<"\nMax";cin >>Max;

randomize();

cout <<"\n\nResults:\n";

for(int i=0;i<20;i++){

RandomNumber=rand(Min,Max)

cout <<"\n"<<RandomNumber;

}

getch();

return 0;

}

//------------------------

Firstly, these require:

#include <time.h> //used to seed the random-number generator

You'd need the standard library for rand(), but if you're using CStrings, you're using MFC, so you don't need to explicitly #include it.

Now, on to the function!

int random(int min, int max)

{

static bool firstrun=true; //Is this the first call to this function?

if (firstrun) //if so...

{

//When called the first time, seeds the random-number

//generator with the current time so that the numbers will

//be different each time you run it.

srand( (unsigned)time( NULL ) );

firstrun=false; //don't call this again

}

//This looks more complicated than it is. ^_^

int range = max - min + 1; //sets the size of your range PLUS ONE (important)

return int(min + ( range * rand() / (RAND_MAX + 1.0) ));

/* The explanation:

The variable "range" is the size of the range you are using.

For example, if min=5 and max=10, range==6 (eg, 10-5+1)

rand() / (RAND_MAX + 1.0) gives you a random value between [0..1)

That is to say, somewhere between 0.0 and up to (but not including) 1.0

When we multiply that by our range, in this case we get a

random value between [0..6)

Lastly, we add our minimum value, leaving us with a final random

value between [5..11)

If we convert this to an int, everything past the decimal point is

chopped off, leaving us with an integer value between [5..10],

inclusive. If you DON'T want to convert to int, change the range

so that it does not add one.

*/

}

For doubles, here's the same function (sans comments)

double drandom(double min, double max)

{

static bool firstrun=true;

if (firstrun)

{

srand( (unsigned)time( NULL ) );

firstrun=false;

}

double range = max - min;

return (min + ( range * rand() / (RAND_MAX + 1.0) ));

}

Now, as to converting to a CString, it's super easy. ^_^

CString has a built-in function called Format, and it works just like printf.

So, given a CString str, to convert an int, just use:

str.Format("%d",blah); where blah is the int. For example:

str.Format("%d",random(10,

(This sets the CString str to an integer value between 10 and 20)

If you would rather use floating random numbers, simply change your format string to reflect that:

str.Format("%.2f",drandom(

(Sets str to a value between 10 and 20 with two digits of precision)

double rand(int Min,int Max) {

int Result;

Result=random(Max);

while(Result <=Min){

Result=random(Max);

}

return Result;

}

LOL. That is very unoptimalizated funcation. Which school teachs to write like this ?

That's why some applications are really slow...

Applicationmaker wrote:

double rand(int Min,int Max) {

int Result;

Result=random(Max);

while(Result <=Min){

Result=random(Max);

}

return Result;

}

LOL. That is very unoptimalizated funcation. Which school teachs to write like this ?

That's why some applications are really slow...

[/QUOTE]

let's try this function

rand(99999,100000);

see how many long the while loop runs

I do not disagree that both ozo and jkr provided code that could generate the specific random numbers. However, neither provided explanations explaining how their code works, and neither addressed the author's additional request regarding cstrings.

As someone with administrative powers, you also have a great responsibility, to both the authors and the experts. Truly, it was the author's responsibility to choose whichever answer best fit their needs, and if brightwood had chosen ozo's or jkr's answers, I would have said nothing. But, brightwood has not returned, and if it is up to you to cleanup this question, I would ask that you choose the most complete answer, not simply the first. Indeed, the author's single comment would seem to indicate that ozo's answer, at least, was not satisfactory.

determine if that was helpful is the asker. However, the asker did ask also, how to get that value into a CString.

pcgabe is the only one who helped with that and for that, I believe he should be cut in on the split. I think that

since he is the only one whom responded to that part of the question at all, if the asker had not left, the asker

would have rewarded him points in the split.

By clicking you are agreeing to Experts Exchange's Terms of Use.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**12** Experts available now in Live!