Sign in to follow this  

Help in random-number generator

This topic is 2631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.

I have been able to create a random number, using my random number generator every time i open my application.


However, in my loop code below, my intention was to generate 10 different random numbers, but it will only generate ONE random number and show the same thing 10 consecutive times.

I tried googling it, but to no avail.

Is there a way for my program to show 10 consecutive random numbers in one instance of the application? How should I correct my code?

Here is my code below:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{

int man;

srand(time(0));

man = rand() % 100 +1;

int counter;

for(counter = 0;counter < 15;counter++)
{

cout << man << "\n";

}

return 0;

}


NOTE: the variable that holds the random number is the variable 'man'



any help is appreciated. thanks.

Share this post


Link to post
Share on other sites
Look what's actually happening in your for loop. All it's doing is calling cout 10 times. You need to move the rand() call into there as well, so that it will also be called 10 times.

Share this post


Link to post
Share on other sites

Well, what the program is doing is expected behavior really. You generate a single random number using rand() and assign this particular number to the variable man. This variable now 'contains' this number and printing it will always print this same number; it won't magically re-evaluate rand(), that's simply not how variables work.

To fix the problem, you should call (and optionally assign) rand within your loop:


// this is now redundant
man = rand() % 100 +1;

int counter;

for(counter = 0;counter < 15;counter++)
{
// a new random number is generated and assigned each loop iteration now
man = rand() % 100 +1;
cout << man << "\n";

}




[Edited by - remigius on September 29, 2010 3:44:33 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Zenix
Look what's actually happening in your for loop. All it's doing is calling cout 10 times. You need to move the rand() call into there as well, so that it will also be called 10 times.


you're right. I missed that out. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by remigius

Well, what the program is doing is expected behavior really. You generate a single random number using rand() and assign this particular number to the variable man. This variable now 'contains' this number and printing it will always print this same number; it won't magically re-evaluate rand(), that's simply not how variables work.

To fix the problem, you should call (and optionally assign) rand within your loop:


// this is now redundant
man = rand() % 100 +1;

int counter;

for(counter = 0;counter < 15;counter++)
{
// a new random number is generated and assigned each loop iteration now
man = rand() % 100 +1;
cout << man << "\n";

}




Amazing! No wonder it didn't work out. I just had to put the rand IN the LOOP. Plus, you made the code more efficient by putting the value of man (which is produced by the rand syntax) inside the loop.

Awesome sauce! Thanks.

Share this post


Link to post
Share on other sites
The code is not more efficient, it is more correct. Efficiency can only really be compared with two solutions of equal correctness, or correct to within some reasonable limit.

The two programs do totally different things, so comparing how fast they might do it is meaningless. Of course, in this case there is no measurable efficiency change anyway, the program spends all its time waiting on I/O.

Many new programmers mistakenly obsess over performance. The hard truth is, it takes a lot of experience to know how to really change code to make it faster. I made the same mistake myself. Looking back I think I would have learned better if I had been as focused on correctness.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
The code is not more efficient, it is more correct. Efficiency can only really be compared with two solutions of equal correctness, or correct to within some reasonable limit.

The two programs do totally different things, so comparing how fast they might do it is meaningless. Of course, in this case there is no measurable efficiency change anyway, the program spends all its time waiting on I/O.

Many new programmers mistakenly obsess over performance. The hard truth is, it takes a lot of experience to know how to really change code to make it faster. I made the same mistake myself. Looking back I think I would have learned better if I had been as focused on correctness.


Thank you for clearing up the difference between efficiency and correctness. So i have to correct myself and say that the recent code that was posted there was more correct.

Man, reading the book is one thing, learning from peers is another thing. Plus this forum is active with a well-sized community, i guess i will stick around here for a while.

Share this post


Link to post
Share on other sites

This topic is 2631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this