![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2005
Posts: 18
Rep Power: 0
![]() |
tossing coin, logic problem ?
for (int i=0; i<100; i++)
{ if(toss()>=1) head++; else tail++; } bool toss() { srand(time(0)); return rand()%2; } logic problem ? i cant figure out the problem. can somebody tell me the what happened ? why i stil get the same result each time i call the toss function ? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
call srand() from your main function only once. The reason you're getting the same result is that the for loop is executed within a few milli-seconds and the seed is set to almost the same time, resulting in the same random number each time.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
InfoGeek is right, and here is an optimization:
return rand() & 1; This will do the same as % 2, but it is a lot faster. It masks out all the other bits, so all what remains is a 1 or a 0. |
|
|
|
|
|
#4 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Note that the least significant bit is the only way to make a number odd due to the fact that all the other bit values are even. Modulus is quite a bit slower. Dividing versus logical AND.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
It's actually a [u]bitwise[u] AND (&), not a logical AND (&&).
Some optimising compilers will actually do this sort of optimisation for you (i.e. if they see x%2 where 2 is a compile time constant they will convert it to x&1). |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
Also next time please post the full source.
__________________
Geeks may not be cool now but in the long run they prosper. |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
@teencoder: Why, the code he gave us is enough to solve the problem. His only fault was not putting his code in [code] tags.
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
Oops sorry I didn't see the toss function earlier sometimes I miss things. I guess I'm used to seeing all my functions precede the main.
__________________
Geeks may not be cool now but in the long run they prosper. |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Expert Programmer
|
No, he is correct, &1 is faster than %2.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|