Author Topic: Help?  (Read 5984 times)

0 Members and 1 Guest are viewing this topic.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Help?
« 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:

Code: [Select]
#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
Code: [Select]
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?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #1 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:

Code: [Select]
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] << " ";
}
« Last Edit: October 14, 2006, 05:53:58 pm by Sidoh »

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Help?
« Reply #2 on: October 14, 2006, 05:33:10 pm »
Yeah, that's right for the most part.

This is what finally got what I needed:

Code: [Select]
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.
« Last Edit: October 14, 2006, 05:34:57 pm by AntiVirus »
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #3 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)!
« Last Edit: October 14, 2006, 06:12:48 pm by Sidoh »

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Help?
« Reply #4 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.

Code: [Select]
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] << " ";
}
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #5 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.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Help?
« Reply #6 on: October 14, 2006, 05:56:47 pm »
Hrmm.. So why don't you do rand()%25 + 1?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #7 on: October 14, 2006, 06:00:46 pm »
Hrmm.. So why don't you do rand()%25 + 1?

Because that would be random integers between 1 and 25.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Help?
« Reply #8 on: October 14, 2006, 06:02:51 pm »
What about rand()%26?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #9 on: October 14, 2006, 06:09:00 pm »
What about rand()%26?

0 and 25.

Edit: here, think about it like this.

Code: [Select]
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.

Code: [Select]
rand += 5;
« Last Edit: October 14, 2006, 06:11:40 pm by Sidoh »

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Help?
« Reply #10 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?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #11 on: October 14, 2006, 06:46:15 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].

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 ).

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Help?
« Reply #12 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

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #13 on: October 14, 2006, 09:07:40 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! >:(

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: Help?
« Reply #14 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?
« Last Edit: October 14, 2006, 10:25:59 pm by AntiVirus »
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #15 on: October 14, 2006, 10:25:59 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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Help?
« Reply #16 on: October 15, 2006, 12:25:07 am »
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.

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Help?
« Reply #17 on: October 15, 2006, 12:58:33 am »
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!