Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 2nd, 2006, 1:39 AM   #1
cairo
Newbie
 
Join Date: Dec 2005
Posts: 18
Rep Power: 0 cairo is on a distinguished road
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 ?
cairo is offline   Reply With Quote
Old Jan 2nd, 2006, 2:52 AM   #2
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Old Jan 2nd, 2006, 4:37 AM   #3
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
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.
Polyphemus_ is offline   Reply With Quote
Old Jan 2nd, 2006, 12:49 PM   #4
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Jan 2nd, 2006, 6:08 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
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).
grumpy is offline   Reply With Quote
Old Jan 2nd, 2006, 10:43 PM   #6
teencoder
Hobbyist Programmer
 
teencoder's Avatar
 
Join Date: Jul 2005
Posts: 158
Rep Power: 0 teencoder is an unknown quantity at this point
Also next time please post the full source.
__________________
Geeks may not be cool now but in the long run they prosper.
teencoder is offline   Reply With Quote
Old Jan 2nd, 2006, 10:59 PM   #7
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
@teencoder: Why, the code he gave us is enough to solve the problem. His only fault was not putting his code in [code] tags.
nindoja is offline   Reply With Quote
Old Jan 3rd, 2006, 4:20 AM   #8
teencoder
Hobbyist Programmer
 
teencoder's Avatar
 
Join Date: Jul 2005
Posts: 158
Rep Power: 0 teencoder is an unknown quantity at this point
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.
teencoder is offline   Reply With Quote
Old Jan 3rd, 2006, 6:20 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by Polyphemus_
This will do the same as % 2, but it is a lot faster.
I'm doubtful of this. Surely such a simple optimization would be included in any modern compiler?
Arevos is offline   Reply With Quote
Old Jan 3rd, 2006, 6:22 AM   #10
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC