Velocity Reviews > C++ > rand() between m and n

# rand() between m and n

Gary Wessle
Guest
Posts: n/a

 08-01-2006

Hi

I need help to generate some random numbers between 2 and 8.

#include <cstdlib>
using std::rand;

the following was out of my range,

int main() {

for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << '\t' << endl;
}
}

it can be any quality random number.

thanks

Marco Wahl
Guest
Posts: n/a

 08-01-2006
Gary Wessle <(E-Mail Removed)> writes:
> I need help to generate some random numbers between 2 and 8.

So you need random numbers out of 3, 4, 5, 6, 7, right?

> the following was out of my range,

Did you get all zeros?

> int main() {
>
> for (int i = 0; i < 50; i++){
> int x = (int(rand())/444489786)*8;
> cout << x << '\t' << endl;
> }
> }
>
> it can be any quality random number.

So the rand function should suffice.

Converting the result of the rand function to float
allows you to normalize the rand result into the
interval [0, 1] as floating point number. Then you can
multiply with five (you want to pick a number from a
range of five numbers, don't you?) and then round and

For some code example see e.g.

HTH
--
Marco Wahl
http://visenso.com

Gernot Frisch
Guest
Posts: n/a

 08-01-2006

> I need help to generate some random numbers between 2 and 8.
>
> #include <cstdlib>
> using std::rand;
>
> the following was out of my range,
>
> int main() {
>
> for (int i = 0; i < 50; i++){
> int x = (int(rand())/444489786)*8;
> cout << x << '\t' << endl;
> }
> }

// return random number elm[from; upto]
int rnd_range( int from, int upto)
{
return (rand() % (upto - from + 1)) + from;
}

Chris Theis
Guest
Posts: n/a

 08-01-2006

"Gernot Frisch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>> I need help to generate some random numbers between 2 and 8.
>>
>> #include <cstdlib>
>> using std::rand;
>>
>> the following was out of my range,
>>
>> int main() {
>>
>> for (int i = 0; i < 50; i++){
>> int x = (int(rand())/444489786)*8;
>> cout << x << '\t' << endl;
>> }
>> }

>
> // return random number elm[from; upto]
> int rnd_range( int from, int upto)
> {
> return (rand() % (upto - from + 1)) + from;
> }
>

This works absolutely fine, but still the other approach using a random
number between r out of [0,1) ( x = Min + r * (Max-Min) ) is favorable
because it doesn't tamper with the "randomness" of the generator and has no
influence on the period of the generated sequence.

Cheers
Chris

Gary Wessle
Guest
Posts: n/a

 08-01-2006
"Chris Theis" <(E-Mail Removed)> writes:

> "Gernot Frisch" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> >> I need help to generate some random numbers between 2 and 8.
> >>
> >> #include <cstdlib>
> >> using std::rand;
> >>
> >> the following was out of my range,
> >>
> >> int main() {
> >>
> >> for (int i = 0; i < 50; i++){
> >> int x = (int(rand())/444489786)*8;
> >> cout << x << '\t' << endl;
> >> }
> >> }

> >
> > // return random number elm[from; upto]
> > int rnd_range( int from, int upto)
> > {
> > return (rand() % (upto - from + 1)) + from;
> > }
> >

>
> This works absolutely fine, but still the other approach using a random
> number between r out of [0,1) ( x = Min + r * (Max-Min) ) is favorable

do you mean
int x = from + rand() * (to-from) ?

> because it doesn't tamper with the "randomness" of the generator and has no
> influence on the period of the generated sequence.
> Cheers
> Chris

I am not sure what wrong I am doing, followed the link provided by
Marco, which was

the following puts out zeros on my screen. only zeros no mater how
many times I run it.

#include <ctime>
using std::time;

int main() {

int x;
const int N = 100;
srand( static_cast<unsigned> (time( NULL )) );
for (int i = 0; i < 4; i++) {
x = static_cast<int> ( N*rand())/(RAND_MAX+1) ;
cout << x << endl;
}
}

Marcus Kwok
Guest
Posts: n/a

 08-01-2006
Gary Wessle <(E-Mail Removed)> wrote:
> I am not sure what wrong I am doing, followed the link provided by
> Marco, which was
>
> the following puts out zeros on my screen. only zeros no mater how
> many times I run it.

#include <iostream>

> #include <ctime>
> using std::time;

using std::cout;

> int main() {
>
> int x;
> const int N = 100;
> srand( static_cast<unsigned> (time( NULL )) );
> for (int i = 0; i < 4; i++) {
> x = static_cast<int> ( N*rand())/(RAND_MAX+1) ;

x = static_cast<int>(N*rand()) / (RAND_MAX+1);

So, you are taking the value of N*rand(), and then casting it to an int.
Then you are dividing it by (RAND_MAX+1), which is also an int.
Therefore you have an int/int, so the division truncates, resulting in a
zero.

I would try casting the result of N*rand() to a double, then performing
the (floating-point) division, then casting THAT result to an int:

x = static_cast<int>(static_cast<double>(N*rand()) / (RAND_MAX+1));

Of course, you could break this up into smaller steps.

> cout << x << endl;
> }
> }

--
Marcus Kwok
Replace 'invalid' with 'net' to reply

Gary Wessle
Guest
Posts: n/a

 08-02-2006
http://www.velocityreviews.com/forums/(E-Mail Removed)lid (Marcus Kwok) writes:

> Gary Wessle <(E-Mail Removed)> wrote:
> > I am not sure what wrong I am doing, followed the link provided by
> > Marco, which was
> >
> > the following puts out zeros on my screen. only zeros no mater how
> > many times I run it.

>
> #include <iostream>
>
> > #include <ctime>
> > using std::time;

>
> using std::cout;
>
> > int main() {
> >
> > int x;
> > const int N = 100;
> > srand( static_cast<unsigned> (time( NULL )) );
> > for (int i = 0; i < 4; i++) {
> > x = static_cast<int> ( N*rand())/(RAND_MAX+1) ;

>
>
> x = static_cast<int>(N*rand()) / (RAND_MAX+1);
>
> So, you are taking the value of N*rand(), and then casting it to an int.
> Then you are dividing it by (RAND_MAX+1), which is also an int.
> Therefore you have an int/int, so the division truncates, resulting in a
> zero.
>
> I would try casting the result of N*rand() to a double, then performing
> the (floating-point) division, then casting THAT result to an int:
>
> x = static_cast<int>(static_cast<double>(N*rand()) / (RAND_MAX+1));
>
> Of course, you could break this up into smaller steps.
>
> > cout << x << endl;
> > }
> > }

>
> --
> Marcus Kwok
> Replace 'invalid' with 'net' to reply

do you mean this, it still puts out all zeros, could you try it on
your machine and report back? thanks

****************
****************
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {

srand( static_cast<unsigned> (time( NULL )) );
int N = 8;
int x;
for (unsigned i=0; i<5; i++){
x = static_cast<int>(static_cast<double>(N*rand()) / (RAND_MAX+1));
cout << x << '\t';
}
}
****************
****************

Marco Wahl
Guest
Posts: n/a

 08-02-2006
Gary Wessle <(E-Mail Removed)> writes:

> do you mean this, it still puts out all zeros, could you try it on
> your machine and report back? thanks
>
>
> #include <iostream>
> #include <cstdlib>
>
> using namespace std;
>
> int main() {
>
> srand( static_cast<unsigned> (time( NULL )) );
> int N = 8;
> int x;
> for (unsigned i=0; i<5; i++){
> x = static_cast<int>(static_cast<double>(N*rand()) / (RAND_MAX+1));
> cout << x << '\t';
> }
> }

I checked and also get all 0s. The output looks more interesting when using the line

x = static_cast<int>(N * static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1));

There is an overflow on my machine at least when calculating RAND_MAX+1.

HTH
--
Marco Wahl
http://visenso.com

arash.koushkestani@gmail.com
Guest
Posts: n/a

 08-02-2006
Hello all,
why you are going far away?
first use srand() to randomize radom number, srand get a parameter as
seed, like:
srand(3); or you can use srand(time(0));
then use this method to have randome number in a certain range:
x=minum number +rand()% maximum number;
for example: x=1+rand()%6; gives you a random number between 1 to 6;
Also you mist use srand() before using rand, you can use it at the
first of main()

Gary Wessle
Guest
Posts: n/a

 08-02-2006
the thing is if you need to generate say 5 random numbers between 2
and 8 then this will not work, run this

for (int i= 0; i< 5; i++){
srand( time( 0));
int x= 2+ rand() %8;
cout << x << '\n';
}