0

i want to generate random no. every time i execute the program and generated random sequence should not be identical. here the program i created , generates random sequence and the problem is the generated random sequence is identical every time i execute the program.

#include<stdio.h>
#include<time.h>
#include <stdlib.h>
int main()
{
    time_t t1,t2;
    time(&t1);
    time(&t2);
    int a,b,r,s,score=0,right=0,wrong=0;
    while(t2-t1<100)
    {                   
        a=rand()%100;    // generate random values 0 to 99
        b=rand()%100;
        r=a+b;
        printf("%d + %d = ",a,b);
        scanf("%d",&s);
        if(s==r)
        {
            score=score+10;
            right++;
            printf("        Right!\n");
        }
        else
        {
            score=score-10;
            wrong++;
            printf("        Wrong!\n");
        }

        time(&t2);      
    }
    printf("Your score is %d \n right = %d \n wrong = %d\n",score,right,wrong);
}

Every time i execute the program , generate same sequence 41+67= , 34+0= , 69+24= , 78+58= , ... .

i want this program to generate different expressions ,every time it executes, i don't know how to resolve this issue.

siddstuff
  • 1,215
  • 4
  • 16
  • 37
  • 1
    That's why they are called pseudo-random numbers. If you don't save your status, you will always start from the same beginning. Use time related functions instead. – Cob013 Jun 23 '13 at 18:01
  • 2
    Did you look at `man 3 rand`? You should consider running `srand` with a seed before you start your random "sequence". For a seed, you can use something like the current time in seconds (see `man 3 time`). – lurker Jun 23 '13 at 18:03

0 Answers0