-1

I am trying to better use and understand functions and in this case I need to figure out how to make this function in particular return 2 different random numbers.I have set up the ctime and can successfully call my function and make my variable(message) equal the random number but when I call it again and ask it to print out the newest random number they are both the same.

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

int RandomNumberGen (int x);

int main()
{
    srand(unsigned(time(0)));

    int Ran;
    int message;

    message = RandomNumberGen (Ran);

    cout << "Number 1 " << message << endl;
    message = RandomNumberGen (Ran);

    cout << "Number 2 " << message << endl;
    return 0;
}

int RandomNumberGen (int Ran)
{
    unsigned int RandomNum = 0;
    RandomNum = rand()%8 + 4;

    return RandomNum;
}

As you can see I set the function to output a random number and write it on screen and then I call the same function again and write that one on screen(write the second # on screen).Yet every time I call the function both numbers are written on the screen as the same even though I am generating a random number each time.

I know this is a simple and easy task but please let me know if what I am attempting to do is possible or do I need another separate function for the 2nd number.

My end goal is to base a lot of events and things off of one random number function so I can essentially call the function for a random number and then let it determine what happens next.

I placed in the code as you asked.I hope this is what you meant.I appreciate all the help and am very grateful for the answers.I plan to be using this more so I will be sure to get it right for you guys!

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Mrmug
  • 11
  • 4
  • 4
    Please don't post pictures of your code. Edit the question and copy-paste the code in there. Make sure you format it as code using the button above the edit box. – Praetorian Aug 30 '14 at 00:36
  • 1
    Use the `` header. – chris Aug 30 '14 at 00:37
  • I notice that even in the image, you didn't format your code well. Since you are only starting to learn programming, keeping a good habit is important. – Yu Hao Aug 30 '14 at 00:43
  • I wish I could bump chris' comment a hundred times. Seriously. If your toolchain and lib support C++11 [****](http://en.cppreference.com/w/cpp/numeric/random), *use it*. Once you use ``, there is simply no going back to `srand()` and `rand()`; *ever*. – WhozCraig Aug 30 '14 at 00:45
  • This question appears to be off-topic because OP did not bother to copy the code, but posted a screenshot, still with a messed indentation and way too many newlines. – Kijewski Aug 30 '14 at 00:45
  • @WhozCraig, Until you find a new job where they don't use C++11 or Boost :p – chris Aug 30 '14 at 00:47
  • @chris I already have one of those, and I've been dragging the neanderthals with a mile-long tow-rope trying to pull them to the light for almost two years now. Talk about a slow process. I think I may *finally* get them to move forward from VS2010 on the next major release. – WhozCraig Aug 30 '14 at 00:49
  • @WhozCraig, Hehe, good luck. I think I'm due to use C90 in a few months. – chris Aug 30 '14 at 00:52
  • @chris heh. you enjoy that. I'll consider myself fortunate to be in the grey-ages. beats being where you're about to be =P – WhozCraig Aug 30 '14 at 00:53
  • @Mrmug [C++11 version](http://ideone.com/Rj2NpT), in case you're interested. Best of luck. – WhozCraig Aug 30 '14 at 00:58
  • I'm voting to close this question as off-topic because the original question was poorly composed and the edits have changed the question completely. It simply cannot be saved. – David Schwartz Oct 11 '16 at 08:11

3 Answers3

1

You called srand in the function RandomNumberGen, which is called multiple times. That's wrong, srand should be called only once, try put it in main instead.

Or better, instead of the C library crandom functions, use the methods in random if that's available.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

Take a look at this question and particularly the answer. It should clarify some things about srand() and rand()

I quote the answer:

Seed is usually taken from the current time, which are the seconds, as in time(NULL), so if you always set the seed before taking the random number, you will get the same number as long as you call the srand/rand combo multiple times in the same second.

Basically what you should do, is call srand() only once at the beginning of your application and not in the function. Because your function will be called twice at a very short interval. Almost all the time in the same second. Generating the same starting sequence for the rand() function

Community
  • 1
  • 1
Mathieu David
  • 4,706
  • 3
  • 18
  • 28
1

you shouldn't seed the random number generator with each call, but only once. your two calls to the function probably execute so fast that the value of time(null) is still the same, and the same seed will produce the same pseudo random sequence.
(btw., you should use null or NULL, not 0 for a null pointer, and you do not use the parameter you pass to your randomnumbergen function, so you could just remove it entirely.)

ths
  • 2,858
  • 1
  • 16
  • 21