Question

I'm looking at a Qt specific C++ solution for the typical producer/consumer problem. Here's the code for the producer:

class Producer : public QThread
{
public:
    void run()
    {
        qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
        for (int i = 0; i < DataSize; ++i) {
            freeBytes.acquire();                                 // (1)
            buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4];   // (2)
            usedBytes.release();
        }
    }
};

I am not able to understand the second line in the for loop viz. "ACGT"[*] syntax. What does it do exactly? Is this Qt specific or is this C++ syntax I'm not aware of?

PS: Full source code here

Was it helpful?

Solution

It generates a random characted from: A, C, G, T.

Literal "ACGT" is an array of type char const [5] then [(int)qrand() % 4] is a random index in a range from 0 to 3 including.

OTHER TIPS

"ACGT"[*] syntax. What does it do exactly?

qrand() % 4 is a random number between 0 and 3. That random number is used as an index into the string "ACGT". So the whole expression yields a random character A, C, G or T with equal probability.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top