Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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
add three to the result.

For some code example see e.g.

http://cplus.about.com/od/advancedtu.../aa041303c.htm


HTH
--
Marco Wahl
http://visenso.com
 
Reply With Quote
 
 
 
 
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;
}




 
Reply With Quote
 
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


 
Reply With Quote
 
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
http://cplus.about.com/od/advancedtu.../aa041303c.htm

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;
}
}
 
Reply With Quote
 
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
> http://cplus.about.com/od/advancedtu.../aa041303c.htm
>
> 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) ;


Let's rearrange your spacing:

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
 
Reply With Quote
 
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
> > http://cplus.about.com/od/advancedtu.../aa041303c.htm
> >
> > 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) ;

>
> Let's rearrange your spacing:
>
> 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';
}
}
****************
****************
 
Reply With Quote
 
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
 
Reply With Quote
 
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()
Hope I could help you;

 
Reply With Quote
 
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';
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
Subnets between core and access/distribution layers & routing between layers J Cisco 0 12-13-2006 08:38 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments