Returning a random number from the function

Feb 10, 2011 at 2:59pm
Hello guys.
Please advice how to get a random number from the function. I have to "throw a coin" one hundred times and count heads and tails amount. I did it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<ctime.h>
#include<cstdlib.h>
#pragma hdrstop
int flip ();
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{ int heads = 0, tails = 0, fart ;

  for (int i =1;i<=100;i++) {
      fart = flip();
      cout << fart << " ";
      if (fart == 0)
          heads++;
      else
          tails++;}

 cout << heads << " " << tails;

getch();
        return 0;
}
//---------------------------------------------------------------------------
int flip ()
{
 randomize();
 int mod = rand()%2;
 return mod;
}

It doesn't matter if i use randomize() or srand(time(0)). The result is the same. It gives me either zeros or ones only while i need both. What am i doing incorrectly?
Feb 10, 2011 at 3:12pm
closed account (z05DSL3A)
randomize() or srand(time(0)) should be at the start of main and only called once.
Feb 10, 2011 at 7:23pm
Thank you very much, Grey Wolf
Topic archived. No new replies allowed.