Help in random-number generator

Started by
5 comments, last by Bladelock 13 years, 6 months ago
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.
Advertisement
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.

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 redundantman = rand() % 100 +1;int counter;for(counter = 0;counter < 15;counter++){// a new random number is generated and assigned each loop iteration nowman = rand() % 100 +1;cout << man << "\n";}



[Edited by - remigius on September 29, 2010 3:44:33 AM]
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
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.
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 redundantman = rand() % 100 +1;int counter;for(counter = 0;counter < 15;counter++){// a new random number is generated and assigned each loop iteration nowman = 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.

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

This topic is closed to new replies.

Advertisement