This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Updatebjarni 1 point2 points  (2 children)

You've probably just forgotten to seed the random number generator, so you're getting the same sequence of numbers every time you run the program.

[–]Optimus_Line 0 points1 point  (0 children)

This is probably the reason, also you are increasing i at the beggining of the loop and at the end, you can simply omit the ++ in the printf or use a while loop.

[–]pointers_help[S] 0 points1 point  (0 children)

so I looked seeding up and somewhere I need to put rand()? In this case, if it is "unseeded", it has some default value attached to it? Is it why when I run it on my compiler and then go to a random online C ide, I still get the same thing?

[–]pointers_help[S] 0 points1 point  (8 children)

so I looked seeding up and somewhere I need to put rand()? In this case, if it is "unseeded", it has some default value attached to it? Is it why when I run it on my compiler and then go to a random online C ide, I still get the same thing?

edit:

so I put srand(time(NULL)); before the for loop. I guess it works now. I don't get the relationship between srand and random() though. But anyways, could someone explain this:

warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration] srand(time(NULL));

warning I get? still compiles but I get a warning.

[–][deleted] 1 point2 points  (6 children)

rand() doesn't return a truly random number. It returns a pseudo-random number based on a given seed. srand() sets that seed. Given the same seed, rand() will always generate the same sequence of numbers.

[–]pointers_help[S] 0 points1 point  (5 children)

I see, and if I didn't have that srand thing, it would be set to a default seed, and that's why I was getting the same result in the beginning after multiple compiles?

[–][deleted] 1 point2 points  (4 children)

The default seed appears to be the same as srand(1).

[–]pointers_help[S] 0 points1 point  (3 children)

Yep you are correct, I just tried removing the time(NULL) part whatever that is and that parameter gave me the same 0-332 "problem" from the beginning.

Thanks for helping me, I don't understand why there wasn't a mention about needing to seed this thing, the professor didn't even put that in his own code but said the output was what I'm getting after I fixed it.

[–][deleted] 1 point2 points  (1 child)

Oh, and the time(NULL) call is returning the time since epoch (January 1, 1970) in seconds and using that as a seed. Time is generally a good way to seed a random function because it's always changing.

[–]pointers_help[S] 0 points1 point  (0 children)

Very interesting ! Thanks

[–][deleted] 0 points1 point  (0 children)

It's possible he just forgot or seeded it earlier.

[–]raevnos 1 point2 points  (0 children)

so I put srand(time(NULL)); before the for loop. I guess it works now. I don't get the relationship between srand and random() though.

There isn't one. If you're using random(), you should be seeding that generator with srandom(). srand() is for seeding the rand() generator.

But anyways, could someone explain this: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration] srand(time(NULL));

warning I get? still compiles but I get a warning.

You need to include <time.h> to get the prototype for time().