So I made this coin flipping program. It generates a random number between 1 and 2, and checks to see how many "flips" it would take for it to get 15 flips in a row. However, every time I run it, I get the same number, 13505, every single time! How can I fix this, so that it doesn't do this every time? I tired changing the value of num to 1-100, but it still comes up with the same answer every time(this time its 11341)

#include <iostream>
#include <string>
#include <time.h>
#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
begin:
system("cls");
system("TITLE Coin Flip");
int i=0;
long int j=0;
while(i<15) 
 {
	int num=rand()%2+1;
	j++;
	if(num==1)
	{
		 i++;
	}
	else if(num==2)
	{
		 i=0;
	}
} 
cout<<j<<endl;
system("pause");
}

there is no such thing as a 100% random number on a computer. and its *supposed* to produce the same sequence every time by default.

to avoid this use

srand ( time(NULL) );

to initialise the random number generator with a seed (the current time)

Can you give me an example, as to how to use this, because I want to set the range of numbers.

Can you give me an example, as to how to use this, because I want to set the range of numbers.

int main()
{
   srand( time( 0 ) );

   cout << rand() % 2 << endl;
}

oh ok thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.