0

I am trying to write a code to return random character so I write the following code:

static char prepare-data()
{
    Sleep(rand()%500); // sleep random amount
    int n = rand()%26;
    char c = (char)(n+65);
    return c; // return random character
}

static void writer()
{
    int i, writePt = 0;
    char data;
    for (i = 0; i < data_length; i++)
    {
        data = preparedata(); // go off & get data ready
        buffers[writePt] = data; // put data into buffer
        cout<<"writer thread: buffer["<<writePt<<"]="<<data<<endl;
        writePt = (writePt + 1) % num_total_buffer;
    }
}

but every time I run the code I get the same characters is this considered as problem

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

4

Maybe you want to add

/* initialize random seed: */
  srand (time(NULL));

before using rand()

Yuanhang Guo
  • 466
  • 4
  • 9
  • Yes, but this is covered in the duplicate questions linked above. No need to answer again. – Floris Mar 27 '14 at 05:23
  • You are right and I will delete this answer. – Yuanhang Guo Mar 27 '14 at 05:27
  • 1
    @YuanhangGuo No need to delete it. The topic will be closed eventually. In the meantime, enjoy the rep you're getting from the upvotes. You don't have much atm, you could use it. :P – cf- Mar 27 '14 at 05:32