Clan x86

Technical (Development, Security, etc.) => General Programming => Topic started by: AntiVirus on October 14, 2006, 04:27:07 PM

Title: Help?
Post by: AntiVirus on October 14, 2006, 04:27:07 PM
This is the first time in C++ that I have used the random number generator function, and I need help.

What I am suppose to do is seed the random number generator with 42 and populate an array with 20 random numbers from 5 to 25 and then output it on a single line.  And I can get it to spit out 20 numbers, but they aren't between 5 and 25.  This is what I have so far:


#include <iostream>
using namespace std;

int main()
{
  int array[20];
  srand(42);

  for(int i = 1; i <= 20; i++)
  {
    if(array[i] >= 5 || array[i] <= 25)
      cout<<rand()<<" ";
  }
return 0;
}


The line if(array[i] >= 5 || array[i] <= 25) I think has something wrong with it.  Why isn't it &&?  If I use && I only get 2 numbers generated to the screen, 175 and 400, whereas if I use || I get 20 random numbers that are huge.

So what am I doing wrong?
Title: Re: Help?
Post by: Sidoh on October 14, 2006, 05:15:35 PM
With your for loop, you're only generating 20 random integers.  You check to see if those random integers are between a certain range, but it's pretty unlikely that they'll all be between 5 and 25.  It should be a logical and comparision.  It doesn't make sense to say if it's greater than five or less than 25 if you want numbers between 5 and 25!

Also, am I missing something or did you never assign random integers to the nodes of the array?  If the nodes aren't initialized, you'd be getting memory addresses, I think.

It should look something like this, I think:


int randoms[20];
srand(42);

for(int i = 0; i < 20; i++)
{
    // Random number between 5 and 25.  Not entirely sure I did this correctly.  Make sure I did!
    // Edit: Changed + 5 to + 6 and % 19 to % 21
    randoms[i] = ( (rand() % 21) + 6 );
    cout << randoms[i] << " ";
}
Title: Re: Help?
Post by: AntiVirus on October 14, 2006, 05:33:10 PM
Yeah, that's right for the most part.

This is what finally got what I needed:


int first_array[20];
srand(42);

for(int i = 0; i <= 25; i++)
{
    //Random numbers between 5 and 25.
    first_array[i] =((rand() % 25) + 1);
     if(first_array[i] >= 5)
       cout << first_array[i] << " ";
}


Thanks.
Title: Re: Help?
Post by: Sidoh on October 14, 2006, 05:42:16 PM
You're assigning elements in the array first_array past its bounds.  You declared it as having 21 elements and, with your for loop, you're assigning it 26 elements.  Remember that in C++ (and most languages) arrays start at node 0, not 1.  I'm still not sure what you're doing with the if statement.  You don't need it!  Just generate random numbers between 5 and 25 right off the bat (with (rand() % 19) + 6(rand() % 21) + 5)!
Title: Re: Help?
Post by: AntiVirus on October 14, 2006, 05:47:05 PM
Mmk, I did what you said, but I don't understand why it's %19 + 6 and not just %25.


int first_array[20];
srand(42);

for(int i = 0; i < 20; i++)
{
    //Random numbers between 5 and 25.
    first_array[i] =((rand() % 19) + 6);
       cout << first_array[i] << " ";
}
Title: Re: Help?
Post by: Sidoh on October 14, 2006, 05:52:43 PM
Oops, it should actually be % 21!  Sorry!

rand() generates a random integer between 0 and some constant defining a maximum.  Since [5 - 25] is the same as saying ([0 - 20] + 5), you can achieve 5 - 25 by restricting the range of rand() to [0 - 20] (rand() % 21) and then adding 5. 

rand() % 25 would yield random integers between 0 and 24.
Title: Re: Help?
Post by: AntiVirus on October 14, 2006, 05:56:47 PM
Hrmm.. So why don't you do rand()%25 + 1?
Title: Re: Help?
Post by: Sidoh on October 14, 2006, 06:00:46 PM
Quote from: AntiVirus on October 14, 2006, 05:56:47 PM
Hrmm.. So why don't you do rand()%25 + 1?

Because that would be random integers between 1 and 25.
Title: Re: Help?
Post by: AntiVirus on October 14, 2006, 06:02:51 PM
What about rand()%26?
Title: Re: Help?
Post by: Sidoh on October 14, 2006, 06:09:00 PM
Quote from: AntiVirus on October 14, 2006, 06:02:51 PM
What about rand()%26?

0 and 25.

Edit: here, think about it like this.

int rand = ( rand() % 21 ); // assign rand a random integer between 0 and 20

So now rand has a value between 0 and 20.  I lied before, it's +5, but % 21.  It's been a while since I've been using languages blessed with nice random functions!

So to get 5 - 25 from 0 - 20, you add 5.

rand += 5;
Title: Re: Help?
Post by: AntiVirus on October 14, 2006, 06:14:16 PM
Ooh.. So rand()%26 would work if I didn't want to restrict the number to greater than 5?

So if I did restrict it, it would be 5-30, right?
Title: Re: Help?
Post by: Sidoh on October 14, 2006, 06:46:15 PM
Quote from: AntiVirus on October 14, 2006, 06:14:16 PM
Ooh.. So rand()%26 would work if I didn't want to restrict the number to greater than 5?

And if you didn't mind including 0 in the list of possible results.  The list of possible values in ( rand() % 26 ) is [0, 25].

Quote from: AntiVirus on October 14, 2006, 06:14:16 PM
So if I did restrict it, it would be 5-30, right?

If you restricted ( rand() % 26 ) to integers greater than 5, the range would be [6, 25].  Greater than or equal to 5 would be [5, 25].  Again, though, you don't want to do that.  Especially with a for loop where your number of iterations is limited by a constant number instead of a condition.  It's way more efficient to just produce random values in the range [5, 25] with ( (rand() % 21) + 5 ).
Title: Re: Help?
Post by: iago on October 14, 2006, 08:20:53 PM
The general formula that I remember is:

x = (rand() % range) + minimum

So if you want between 25 and 30, the range is 5 (30 - 25 = 5) and the minimum is 25, so it'd be x = (rand() % 5) + 25. 

I think that's what Sidoh is saying, but from my quick skim I don't see that exact formula, so.. :P
Title: Re: Help?
Post by: Sidoh on October 14, 2006, 09:07:40 PM
Quote from: iago on October 14, 2006, 08:20:53 PM
The general formula that I remember is:

x = (rand() % range) + minimum

So if you want between 25 and 30, the range is 5 (30 - 25 = 5) and the minimum is 25, so it'd be x = (rand() % 5) + 25. 

I think that's what Sidoh is saying, but from my quick skim I don't see that exact formula, so.. :P

I was trying to explain why the general syntax works so he could figure it out for himself! >:(
Title: Re: Help?
Post by: AntiVirus on October 14, 2006, 10:20:10 PM
So then shouldn't it be:

x = (rang()%20) + 5?

5 - 25 = 20
Min: 5

So why isn't it that?
Title: Re: Help?
Post by: Sidoh on October 14, 2006, 10:25:59 PM
Quote from: AntiVirus on October 14, 2006, 10:20:10 PM
So then shouldn't it be:

x = (rang()%20) + 5?

No!  If you devide something by 20, you can never have remander 20!  Think about what modulus means.
Title: Re: Help?
Post by: iago on October 15, 2006, 12:25:07 AM
Quote from: AntiVirus on October 14, 2006, 10:20:10 PM
So then shouldn't it be:

x = (rang()%20) + 5?

rang()? :P

Sorry, I think I missed a -1 in my formula.  Sorry :P

rand() % 20 returns a random number between 0 and 19, inclusive
+5 makes it 5 to 24.  The range is actually off by one.
Title: Re: Help?
Post by: Sidoh on October 15, 2006, 12:58:33 AM
Quote from: iago on October 15, 2006, 12:25:07 AM
Quote from: AntiVirus on October 14, 2006, 10:20:10 PM
So then shouldn't it be:

x = (rang()%20) + 5?

rang()? :P

Sorry, I think I missed a -1 in my formula.  Sorry :P

rand() % 20 returns a random number between 0 and 19, inclusive
+5 makes it 5 to 24.  The range is actually off by one.

You mean a + 1?  I see why saying -1 is valid too, but it's terribly misleading!