Why does rand() % 7 always return 0?
This seems to be a really strange issue:
This is my code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv)
{
@autoreleasepool {
srand((unsigned int)time(NULL));
int newRandomNumber = 0;
newRandomNumber = rand() % 7;
NSLog(@"%d", rand() % 7); //This prints out what I expected
NSLog(@"newRandomNumber = %d", newRandomNumber); // This always prints out 0!
}
return 0;
}
If I replace that one line that says
newRandomNumber = rand() % 7
with
newRandomNumber = rand() % 8
everything works perfectly. Why is that the case?
objective-c c random
|
show 1 more comment
This seems to be a really strange issue:
This is my code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv)
{
@autoreleasepool {
srand((unsigned int)time(NULL));
int newRandomNumber = 0;
newRandomNumber = rand() % 7;
NSLog(@"%d", rand() % 7); //This prints out what I expected
NSLog(@"newRandomNumber = %d", newRandomNumber); // This always prints out 0!
}
return 0;
}
If I replace that one line that says
newRandomNumber = rand() % 7
with
newRandomNumber = rand() % 8
everything works perfectly. Why is that the case?
objective-c c random
1
Can't reproduce your problem inC.
– Mat
Oct 23 '11 at 14:42
1
I'm able to reproduce this in Objective-C. Even withsrand([[NSDate date] timeIntervalSince1970]). But passing smaller integer tosrand()seems to fix it.
– Lukman
Oct 23 '11 at 14:47
2
What do you get if you print rand()? Do you get the same number each time, or only something that is congruent to 0 modulo 7?
– Omri Barel
Oct 23 '11 at 14:48
Have you tried iterating more than once? I'm guessing that the first rand() value is always a multiple of 7.
– Hot Licks
Nov 17 '13 at 15:44
Based on the link provided by Grady Player, try importing ctime.h
– Hot Licks
Nov 17 '13 at 15:47
|
show 1 more comment
This seems to be a really strange issue:
This is my code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv)
{
@autoreleasepool {
srand((unsigned int)time(NULL));
int newRandomNumber = 0;
newRandomNumber = rand() % 7;
NSLog(@"%d", rand() % 7); //This prints out what I expected
NSLog(@"newRandomNumber = %d", newRandomNumber); // This always prints out 0!
}
return 0;
}
If I replace that one line that says
newRandomNumber = rand() % 7
with
newRandomNumber = rand() % 8
everything works perfectly. Why is that the case?
objective-c c random
This seems to be a really strange issue:
This is my code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv)
{
@autoreleasepool {
srand((unsigned int)time(NULL));
int newRandomNumber = 0;
newRandomNumber = rand() % 7;
NSLog(@"%d", rand() % 7); //This prints out what I expected
NSLog(@"newRandomNumber = %d", newRandomNumber); // This always prints out 0!
}
return 0;
}
If I replace that one line that says
newRandomNumber = rand() % 7
with
newRandomNumber = rand() % 8
everything works perfectly. Why is that the case?
objective-c c random
objective-c c random
edited Nov 17 '13 at 15:40
Peter Mortensen
13.7k1986112
13.7k1986112
asked Oct 23 '11 at 14:36
0xdead10cc0xdead10cc
19626
19626
1
Can't reproduce your problem inC.
– Mat
Oct 23 '11 at 14:42
1
I'm able to reproduce this in Objective-C. Even withsrand([[NSDate date] timeIntervalSince1970]). But passing smaller integer tosrand()seems to fix it.
– Lukman
Oct 23 '11 at 14:47
2
What do you get if you print rand()? Do you get the same number each time, or only something that is congruent to 0 modulo 7?
– Omri Barel
Oct 23 '11 at 14:48
Have you tried iterating more than once? I'm guessing that the first rand() value is always a multiple of 7.
– Hot Licks
Nov 17 '13 at 15:44
Based on the link provided by Grady Player, try importing ctime.h
– Hot Licks
Nov 17 '13 at 15:47
|
show 1 more comment
1
Can't reproduce your problem inC.
– Mat
Oct 23 '11 at 14:42
1
I'm able to reproduce this in Objective-C. Even withsrand([[NSDate date] timeIntervalSince1970]). But passing smaller integer tosrand()seems to fix it.
– Lukman
Oct 23 '11 at 14:47
2
What do you get if you print rand()? Do you get the same number each time, or only something that is congruent to 0 modulo 7?
– Omri Barel
Oct 23 '11 at 14:48
Have you tried iterating more than once? I'm guessing that the first rand() value is always a multiple of 7.
– Hot Licks
Nov 17 '13 at 15:44
Based on the link provided by Grady Player, try importing ctime.h
– Hot Licks
Nov 17 '13 at 15:47
1
1
Can't reproduce your problem in
C.– Mat
Oct 23 '11 at 14:42
Can't reproduce your problem in
C.– Mat
Oct 23 '11 at 14:42
1
1
I'm able to reproduce this in Objective-C. Even with
srand([[NSDate date] timeIntervalSince1970]). But passing smaller integer to srand() seems to fix it.– Lukman
Oct 23 '11 at 14:47
I'm able to reproduce this in Objective-C. Even with
srand([[NSDate date] timeIntervalSince1970]). But passing smaller integer to srand() seems to fix it.– Lukman
Oct 23 '11 at 14:47
2
2
What do you get if you print rand()? Do you get the same number each time, or only something that is congruent to 0 modulo 7?
– Omri Barel
Oct 23 '11 at 14:48
What do you get if you print rand()? Do you get the same number each time, or only something that is congruent to 0 modulo 7?
– Omri Barel
Oct 23 '11 at 14:48
Have you tried iterating more than once? I'm guessing that the first rand() value is always a multiple of 7.
– Hot Licks
Nov 17 '13 at 15:44
Have you tried iterating more than once? I'm guessing that the first rand() value is always a multiple of 7.
– Hot Licks
Nov 17 '13 at 15:44
Based on the link provided by Grady Player, try importing ctime.h
– Hot Licks
Nov 17 '13 at 15:47
Based on the link provided by Grady Player, try importing ctime.h
– Hot Licks
Nov 17 '13 at 15:47
|
show 1 more comment
2 Answers
2
active
oldest
votes
Well, this
int seed;
for(seed = 1; seed < 10; seed++) {
srand(seed);
printf("%4d %16dn", seed, rand());
}
prints
1 16807
2 33614
3 50421
4 67228
5 84035
6 100842
7 117649
8 134456
9 151263
which makes me think that rand() = seed * 16807
Wikipedia article Linear congruential generator confirms that CarbonLib indeed uses Xn+1 = Xn * 16807 to generate random numbers.
1
Nice investigation!
– Josh Caswell
Oct 23 '11 at 17:59
Formula on Linux 2.6.32 I run is more complicated. It's of the form next = ( next * const_1 + const_2) % 32,768. obviously, maybe you reverse-engineered your platform's formula. It's implementation dependent though with constants that often are near-prime after twisting and turning them.
– gnometorule
Oct 23 '11 at 18:12
It's unlikely though to make them simply remainders w/o other transformations first.
– gnometorule
Oct 23 '11 at 18:13
1
what a bad choice of LCG
– phuclv
Apr 18 '15 at 9:00
2
The part you left out: since16807 % 7 == 0,(n * 16807) % 7 == 0as well, as long as you didn't get integer overflow.
– Mark Ransom
Sep 24 '15 at 18:49
add a comment |
It seems unlikely but running some tests, after an srand the first rand seems always to be divisible by 7, at least in an int sized variable.
On several runs I got 1303562743, 2119476443, and 2120232758, all of which mod 7 to 0.
The second rand() works, because it is the second rand(). Throw a rand() before your first rand()... or better yet, use a better random number generator random or arc4rand if available.
Also see Stack Overflow question Why is (rand() % anything) always 0 in C++?.
Grady player pretty much answers your question: this is because it is your first call to a number not truly randomly generated. However, one other point and one fact that still puzzles me:
– gnometorule
Oct 23 '11 at 16:19
(1) if you don't see with stand() at all, the system will always produce the sane number on first call. On my system, a 'next' variable keeps track of the next rand() value, and next is seeded to 1 if no srand() call happens. The value reported back is after adding and multiplying system constants and taking the result mod 32,768. This means you would still get 0 for a certain modulus user chosen on first call, in your case 7, but it could be another number.
– gnometorule
Oct 23 '11 at 16:23
1
(2) the article talks about portability issues, which wouldn't be affected by using a modulus. Essentially it points it that time_t might not be properly castable to an unsigned int; and that it is better to not use mod, instead multiply to get your desired range (in your case, *7/RAND_MAX). As this objective C which I don't know, it might be an issue, but I doubt it. What puzzles me is that the time() value reported back gets updated internally at every global timer interrupt in update_tines() (on the LINUX 2.6.xx kernel in any case), and this should happen way between second ticks.
– gnometorule
Oct 23 '11 at 16:29
Why srand() would report back, as you say it does, essentially what amounts to 7 second increments (I know this isn't precisely what it means, but it seems related), makes me scratch my head.
– gnometorule
Oct 23 '11 at 16:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7866754%2fwhy-does-rand-7-always-return-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, this
int seed;
for(seed = 1; seed < 10; seed++) {
srand(seed);
printf("%4d %16dn", seed, rand());
}
prints
1 16807
2 33614
3 50421
4 67228
5 84035
6 100842
7 117649
8 134456
9 151263
which makes me think that rand() = seed * 16807
Wikipedia article Linear congruential generator confirms that CarbonLib indeed uses Xn+1 = Xn * 16807 to generate random numbers.
1
Nice investigation!
– Josh Caswell
Oct 23 '11 at 17:59
Formula on Linux 2.6.32 I run is more complicated. It's of the form next = ( next * const_1 + const_2) % 32,768. obviously, maybe you reverse-engineered your platform's formula. It's implementation dependent though with constants that often are near-prime after twisting and turning them.
– gnometorule
Oct 23 '11 at 18:12
It's unlikely though to make them simply remainders w/o other transformations first.
– gnometorule
Oct 23 '11 at 18:13
1
what a bad choice of LCG
– phuclv
Apr 18 '15 at 9:00
2
The part you left out: since16807 % 7 == 0,(n * 16807) % 7 == 0as well, as long as you didn't get integer overflow.
– Mark Ransom
Sep 24 '15 at 18:49
add a comment |
Well, this
int seed;
for(seed = 1; seed < 10; seed++) {
srand(seed);
printf("%4d %16dn", seed, rand());
}
prints
1 16807
2 33614
3 50421
4 67228
5 84035
6 100842
7 117649
8 134456
9 151263
which makes me think that rand() = seed * 16807
Wikipedia article Linear congruential generator confirms that CarbonLib indeed uses Xn+1 = Xn * 16807 to generate random numbers.
1
Nice investigation!
– Josh Caswell
Oct 23 '11 at 17:59
Formula on Linux 2.6.32 I run is more complicated. It's of the form next = ( next * const_1 + const_2) % 32,768. obviously, maybe you reverse-engineered your platform's formula. It's implementation dependent though with constants that often are near-prime after twisting and turning them.
– gnometorule
Oct 23 '11 at 18:12
It's unlikely though to make them simply remainders w/o other transformations first.
– gnometorule
Oct 23 '11 at 18:13
1
what a bad choice of LCG
– phuclv
Apr 18 '15 at 9:00
2
The part you left out: since16807 % 7 == 0,(n * 16807) % 7 == 0as well, as long as you didn't get integer overflow.
– Mark Ransom
Sep 24 '15 at 18:49
add a comment |
Well, this
int seed;
for(seed = 1; seed < 10; seed++) {
srand(seed);
printf("%4d %16dn", seed, rand());
}
prints
1 16807
2 33614
3 50421
4 67228
5 84035
6 100842
7 117649
8 134456
9 151263
which makes me think that rand() = seed * 16807
Wikipedia article Linear congruential generator confirms that CarbonLib indeed uses Xn+1 = Xn * 16807 to generate random numbers.
Well, this
int seed;
for(seed = 1; seed < 10; seed++) {
srand(seed);
printf("%4d %16dn", seed, rand());
}
prints
1 16807
2 33614
3 50421
4 67228
5 84035
6 100842
7 117649
8 134456
9 151263
which makes me think that rand() = seed * 16807
Wikipedia article Linear congruential generator confirms that CarbonLib indeed uses Xn+1 = Xn * 16807 to generate random numbers.
edited Nov 17 '13 at 15:48
Peter Mortensen
13.7k1986112
13.7k1986112
answered Oct 23 '11 at 17:34
georggeorg
150k35204300
150k35204300
1
Nice investigation!
– Josh Caswell
Oct 23 '11 at 17:59
Formula on Linux 2.6.32 I run is more complicated. It's of the form next = ( next * const_1 + const_2) % 32,768. obviously, maybe you reverse-engineered your platform's formula. It's implementation dependent though with constants that often are near-prime after twisting and turning them.
– gnometorule
Oct 23 '11 at 18:12
It's unlikely though to make them simply remainders w/o other transformations first.
– gnometorule
Oct 23 '11 at 18:13
1
what a bad choice of LCG
– phuclv
Apr 18 '15 at 9:00
2
The part you left out: since16807 % 7 == 0,(n * 16807) % 7 == 0as well, as long as you didn't get integer overflow.
– Mark Ransom
Sep 24 '15 at 18:49
add a comment |
1
Nice investigation!
– Josh Caswell
Oct 23 '11 at 17:59
Formula on Linux 2.6.32 I run is more complicated. It's of the form next = ( next * const_1 + const_2) % 32,768. obviously, maybe you reverse-engineered your platform's formula. It's implementation dependent though with constants that often are near-prime after twisting and turning them.
– gnometorule
Oct 23 '11 at 18:12
It's unlikely though to make them simply remainders w/o other transformations first.
– gnometorule
Oct 23 '11 at 18:13
1
what a bad choice of LCG
– phuclv
Apr 18 '15 at 9:00
2
The part you left out: since16807 % 7 == 0,(n * 16807) % 7 == 0as well, as long as you didn't get integer overflow.
– Mark Ransom
Sep 24 '15 at 18:49
1
1
Nice investigation!
– Josh Caswell
Oct 23 '11 at 17:59
Nice investigation!
– Josh Caswell
Oct 23 '11 at 17:59
Formula on Linux 2.6.32 I run is more complicated. It's of the form next = ( next * const_1 + const_2) % 32,768. obviously, maybe you reverse-engineered your platform's formula. It's implementation dependent though with constants that often are near-prime after twisting and turning them.
– gnometorule
Oct 23 '11 at 18:12
Formula on Linux 2.6.32 I run is more complicated. It's of the form next = ( next * const_1 + const_2) % 32,768. obviously, maybe you reverse-engineered your platform's formula. It's implementation dependent though with constants that often are near-prime after twisting and turning them.
– gnometorule
Oct 23 '11 at 18:12
It's unlikely though to make them simply remainders w/o other transformations first.
– gnometorule
Oct 23 '11 at 18:13
It's unlikely though to make them simply remainders w/o other transformations first.
– gnometorule
Oct 23 '11 at 18:13
1
1
what a bad choice of LCG
– phuclv
Apr 18 '15 at 9:00
what a bad choice of LCG
– phuclv
Apr 18 '15 at 9:00
2
2
The part you left out: since
16807 % 7 == 0, (n * 16807) % 7 == 0 as well, as long as you didn't get integer overflow.– Mark Ransom
Sep 24 '15 at 18:49
The part you left out: since
16807 % 7 == 0, (n * 16807) % 7 == 0 as well, as long as you didn't get integer overflow.– Mark Ransom
Sep 24 '15 at 18:49
add a comment |
It seems unlikely but running some tests, after an srand the first rand seems always to be divisible by 7, at least in an int sized variable.
On several runs I got 1303562743, 2119476443, and 2120232758, all of which mod 7 to 0.
The second rand() works, because it is the second rand(). Throw a rand() before your first rand()... or better yet, use a better random number generator random or arc4rand if available.
Also see Stack Overflow question Why is (rand() % anything) always 0 in C++?.
Grady player pretty much answers your question: this is because it is your first call to a number not truly randomly generated. However, one other point and one fact that still puzzles me:
– gnometorule
Oct 23 '11 at 16:19
(1) if you don't see with stand() at all, the system will always produce the sane number on first call. On my system, a 'next' variable keeps track of the next rand() value, and next is seeded to 1 if no srand() call happens. The value reported back is after adding and multiplying system constants and taking the result mod 32,768. This means you would still get 0 for a certain modulus user chosen on first call, in your case 7, but it could be another number.
– gnometorule
Oct 23 '11 at 16:23
1
(2) the article talks about portability issues, which wouldn't be affected by using a modulus. Essentially it points it that time_t might not be properly castable to an unsigned int; and that it is better to not use mod, instead multiply to get your desired range (in your case, *7/RAND_MAX). As this objective C which I don't know, it might be an issue, but I doubt it. What puzzles me is that the time() value reported back gets updated internally at every global timer interrupt in update_tines() (on the LINUX 2.6.xx kernel in any case), and this should happen way between second ticks.
– gnometorule
Oct 23 '11 at 16:29
Why srand() would report back, as you say it does, essentially what amounts to 7 second increments (I know this isn't precisely what it means, but it seems related), makes me scratch my head.
– gnometorule
Oct 23 '11 at 16:30
add a comment |
It seems unlikely but running some tests, after an srand the first rand seems always to be divisible by 7, at least in an int sized variable.
On several runs I got 1303562743, 2119476443, and 2120232758, all of which mod 7 to 0.
The second rand() works, because it is the second rand(). Throw a rand() before your first rand()... or better yet, use a better random number generator random or arc4rand if available.
Also see Stack Overflow question Why is (rand() % anything) always 0 in C++?.
Grady player pretty much answers your question: this is because it is your first call to a number not truly randomly generated. However, one other point and one fact that still puzzles me:
– gnometorule
Oct 23 '11 at 16:19
(1) if you don't see with stand() at all, the system will always produce the sane number on first call. On my system, a 'next' variable keeps track of the next rand() value, and next is seeded to 1 if no srand() call happens. The value reported back is after adding and multiplying system constants and taking the result mod 32,768. This means you would still get 0 for a certain modulus user chosen on first call, in your case 7, but it could be another number.
– gnometorule
Oct 23 '11 at 16:23
1
(2) the article talks about portability issues, which wouldn't be affected by using a modulus. Essentially it points it that time_t might not be properly castable to an unsigned int; and that it is better to not use mod, instead multiply to get your desired range (in your case, *7/RAND_MAX). As this objective C which I don't know, it might be an issue, but I doubt it. What puzzles me is that the time() value reported back gets updated internally at every global timer interrupt in update_tines() (on the LINUX 2.6.xx kernel in any case), and this should happen way between second ticks.
– gnometorule
Oct 23 '11 at 16:29
Why srand() would report back, as you say it does, essentially what amounts to 7 second increments (I know this isn't precisely what it means, but it seems related), makes me scratch my head.
– gnometorule
Oct 23 '11 at 16:30
add a comment |
It seems unlikely but running some tests, after an srand the first rand seems always to be divisible by 7, at least in an int sized variable.
On several runs I got 1303562743, 2119476443, and 2120232758, all of which mod 7 to 0.
The second rand() works, because it is the second rand(). Throw a rand() before your first rand()... or better yet, use a better random number generator random or arc4rand if available.
Also see Stack Overflow question Why is (rand() % anything) always 0 in C++?.
It seems unlikely but running some tests, after an srand the first rand seems always to be divisible by 7, at least in an int sized variable.
On several runs I got 1303562743, 2119476443, and 2120232758, all of which mod 7 to 0.
The second rand() works, because it is the second rand(). Throw a rand() before your first rand()... or better yet, use a better random number generator random or arc4rand if available.
Also see Stack Overflow question Why is (rand() % anything) always 0 in C++?.
edited May 23 '17 at 12:19
Community♦
11
11
answered Oct 23 '11 at 15:41
Grady PlayerGrady Player
12.2k14268
12.2k14268
Grady player pretty much answers your question: this is because it is your first call to a number not truly randomly generated. However, one other point and one fact that still puzzles me:
– gnometorule
Oct 23 '11 at 16:19
(1) if you don't see with stand() at all, the system will always produce the sane number on first call. On my system, a 'next' variable keeps track of the next rand() value, and next is seeded to 1 if no srand() call happens. The value reported back is after adding and multiplying system constants and taking the result mod 32,768. This means you would still get 0 for a certain modulus user chosen on first call, in your case 7, but it could be another number.
– gnometorule
Oct 23 '11 at 16:23
1
(2) the article talks about portability issues, which wouldn't be affected by using a modulus. Essentially it points it that time_t might not be properly castable to an unsigned int; and that it is better to not use mod, instead multiply to get your desired range (in your case, *7/RAND_MAX). As this objective C which I don't know, it might be an issue, but I doubt it. What puzzles me is that the time() value reported back gets updated internally at every global timer interrupt in update_tines() (on the LINUX 2.6.xx kernel in any case), and this should happen way between second ticks.
– gnometorule
Oct 23 '11 at 16:29
Why srand() would report back, as you say it does, essentially what amounts to 7 second increments (I know this isn't precisely what it means, but it seems related), makes me scratch my head.
– gnometorule
Oct 23 '11 at 16:30
add a comment |
Grady player pretty much answers your question: this is because it is your first call to a number not truly randomly generated. However, one other point and one fact that still puzzles me:
– gnometorule
Oct 23 '11 at 16:19
(1) if you don't see with stand() at all, the system will always produce the sane number on first call. On my system, a 'next' variable keeps track of the next rand() value, and next is seeded to 1 if no srand() call happens. The value reported back is after adding and multiplying system constants and taking the result mod 32,768. This means you would still get 0 for a certain modulus user chosen on first call, in your case 7, but it could be another number.
– gnometorule
Oct 23 '11 at 16:23
1
(2) the article talks about portability issues, which wouldn't be affected by using a modulus. Essentially it points it that time_t might not be properly castable to an unsigned int; and that it is better to not use mod, instead multiply to get your desired range (in your case, *7/RAND_MAX). As this objective C which I don't know, it might be an issue, but I doubt it. What puzzles me is that the time() value reported back gets updated internally at every global timer interrupt in update_tines() (on the LINUX 2.6.xx kernel in any case), and this should happen way between second ticks.
– gnometorule
Oct 23 '11 at 16:29
Why srand() would report back, as you say it does, essentially what amounts to 7 second increments (I know this isn't precisely what it means, but it seems related), makes me scratch my head.
– gnometorule
Oct 23 '11 at 16:30
Grady player pretty much answers your question: this is because it is your first call to a number not truly randomly generated. However, one other point and one fact that still puzzles me:
– gnometorule
Oct 23 '11 at 16:19
Grady player pretty much answers your question: this is because it is your first call to a number not truly randomly generated. However, one other point and one fact that still puzzles me:
– gnometorule
Oct 23 '11 at 16:19
(1) if you don't see with stand() at all, the system will always produce the sane number on first call. On my system, a 'next' variable keeps track of the next rand() value, and next is seeded to 1 if no srand() call happens. The value reported back is after adding and multiplying system constants and taking the result mod 32,768. This means you would still get 0 for a certain modulus user chosen on first call, in your case 7, but it could be another number.
– gnometorule
Oct 23 '11 at 16:23
(1) if you don't see with stand() at all, the system will always produce the sane number on first call. On my system, a 'next' variable keeps track of the next rand() value, and next is seeded to 1 if no srand() call happens. The value reported back is after adding and multiplying system constants and taking the result mod 32,768. This means you would still get 0 for a certain modulus user chosen on first call, in your case 7, but it could be another number.
– gnometorule
Oct 23 '11 at 16:23
1
1
(2) the article talks about portability issues, which wouldn't be affected by using a modulus. Essentially it points it that time_t might not be properly castable to an unsigned int; and that it is better to not use mod, instead multiply to get your desired range (in your case, *7/RAND_MAX). As this objective C which I don't know, it might be an issue, but I doubt it. What puzzles me is that the time() value reported back gets updated internally at every global timer interrupt in update_tines() (on the LINUX 2.6.xx kernel in any case), and this should happen way between second ticks.
– gnometorule
Oct 23 '11 at 16:29
(2) the article talks about portability issues, which wouldn't be affected by using a modulus. Essentially it points it that time_t might not be properly castable to an unsigned int; and that it is better to not use mod, instead multiply to get your desired range (in your case, *7/RAND_MAX). As this objective C which I don't know, it might be an issue, but I doubt it. What puzzles me is that the time() value reported back gets updated internally at every global timer interrupt in update_tines() (on the LINUX 2.6.xx kernel in any case), and this should happen way between second ticks.
– gnometorule
Oct 23 '11 at 16:29
Why srand() would report back, as you say it does, essentially what amounts to 7 second increments (I know this isn't precisely what it means, but it seems related), makes me scratch my head.
– gnometorule
Oct 23 '11 at 16:30
Why srand() would report back, as you say it does, essentially what amounts to 7 second increments (I know this isn't precisely what it means, but it seems related), makes me scratch my head.
– gnometorule
Oct 23 '11 at 16:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7866754%2fwhy-does-rand-7-always-return-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can't reproduce your problem in
C.– Mat
Oct 23 '11 at 14:42
1
I'm able to reproduce this in Objective-C. Even with
srand([[NSDate date] timeIntervalSince1970]). But passing smaller integer tosrand()seems to fix it.– Lukman
Oct 23 '11 at 14:47
2
What do you get if you print rand()? Do you get the same number each time, or only something that is congruent to 0 modulo 7?
– Omri Barel
Oct 23 '11 at 14:48
Have you tried iterating more than once? I'm guessing that the first rand() value is always a multiple of 7.
– Hot Licks
Nov 17 '13 at 15:44
Based on the link provided by Grady Player, try importing ctime.h
– Hot Licks
Nov 17 '13 at 15:47