void digestBuf() { // Mangle the data for(size_t j = 0; j < 3; j++) { // Taint the bytes GAssert(DIGEST_BYTES >= 32); if(m_pTaint) Taint_buffer(m_pBuf, m_pTaint + (m_rand.next() % (DIGEST_BYTES - 32)), 32, j); // Shuffle the bytes shuffleBytes(m_pBuf, DIGEST_BYTES); // Hash each 64-bit chunk with sha-512 unsigned char* pChunk = m_pBuf; for(size_t i = 0; i < (DIGEST_BYTES / 64); i++) { sha512_ctx ctx; sha512_begin(&ctx); sha512_hash(pChunk, 64, &ctx); sha512_end(pChunk, &ctx); pChunk += 64; } } // Add it to the hash uint64_t* pA = (uint64_t*)m_pBuf; uint64_t* pB = (uint64_t*)m_pHash; for(size_t i = 0; i < (DIGEST_BYTES / sizeof(uint64_t)); i++) { (*pB) += (*pA); pB++; pA++; } }
void Iterate(bool forw) { g_updateView = m_pUpdateDisplay->isChecked(); if(!g_updateView && m_prng->next(8192) == 0) g_updateView = true; m_pModel->IterateModel(m_pBullets->selection(), forw); m_pCanvas->setImage(m_pImage); }
void wipe(unsigned char* pBuf, size_t len) { while(len > 0) { *(pBuf++) = (unsigned char)m_rand.next(); len--; } }
void GLinearRegressor_linear_test(GRand& prng) { // Train GMatrix features1(0, 3); GMatrix labels1(0, 1); for(size_t i = 0; i < 1000; i++) { double* pVec = features1.newRow(); pVec[0] = prng.uniform(); pVec[1] = prng.uniform(); // irrelevant attribute pVec[2] = prng.uniform(); labels1.newRow()[0] = 0.3 * pVec[0] + 2.0 * pVec[2] + 5.0; } GLinearRegressor lr(prng); lr.train(features1, labels1); // Check some values if(lr.beta()->rows() != 1 || lr.beta()->cols() != 3) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[0] - 0.3) > 1e-6) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[1] - 0.0) > 1e-6) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[2] - 2.0) > 1e-6) throw Ex("failed"); if(std::abs(lr.epsilon()[0] - 5.0) > 1e-6) throw Ex("failed"); // Test GMatrix features2(0, 3); GMatrix labels2(0, 1); for(size_t i = 0; i < 1000; i++) { double* pVec = features2.newRow(); pVec[0] = prng.uniform(); pVec[1] = prng.uniform(); // irrelevant attribute pVec[2] = prng.uniform(); labels2.newRow()[0] = 0.3 * pVec[0] + 2.0 * pVec[2] + 5.0; } double results; lr.accuracy(features2, labels2, &results); if(results > 0.0005) throw Ex("failed"); }
void GLinearRegressor_linear_test(GRand& prng) { // Train GMatrix features1(0, 3); GMatrix labels1(0, 1); for(size_t i = 0; i < 1000; i++) { GVec& vec = features1.newRow(); vec[0] = prng.uniform(); vec[1] = prng.uniform(); // irrelevant attribute vec[2] = prng.uniform(); labels1.newRow()[0] = 0.3 * vec[0] + 2.0 * vec[2] + 5.0; } GLinearRegressor lr; lr.train(features1, labels1); // Check some values if(lr.beta()->rows() != 1 || lr.beta()->cols() != 3) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[0] - 0.3) > 1e-6) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[1] - 0.0) > 1e-6) throw Ex("failed"); if(std::abs(lr.beta()->row(0)[2] - 2.0) > 1e-6) throw Ex("failed"); if(std::abs(lr.epsilon()[0] - 5.0) > 1e-6) throw Ex("failed"); // Test GMatrix features2(0, 3); GMatrix labels2(0, 1); for(size_t i = 0; i < 1000; i++) { GVec& vec = features2.newRow(); vec[0] = prng.uniform(); vec[1] = prng.uniform(); // irrelevant attribute vec[2] = prng.uniform(); labels2.newRow()[0] = 0.3 * vec[0] + 2.0 * vec[2] + 5.0; } double rmse = sqrt(lr.sumSquaredError(features2, labels2) / features2.rows()); if(rmse > 0.0224) throw Ex("failed"); }
unsigned char* getHash() { if(m_remaining < DIGEST_BYTES) { while(m_remaining > 0) { *m_pPos = (unsigned char)m_rand.next(); m_pPos++; m_remaining--; } digestBuf(); m_remaining = DIGEST_BYTES; } return m_pHash; }
virtual void doJob(size_t jobId) { // Randomly draw some data (with replacement) GReleaseDataHolder hDrawnFeatures(&m_drawnFeatures); GReleaseDataHolder hDrawnLabels(&m_drawnLabels); for(size_t j = 0; j < m_drawSize; j++) { size_t r = (size_t)m_rand.next(m_features.rows()); m_drawnFeatures.takeRow((GVec*)&m_features[r]); // This case is only okay because we only use drawFeatures as a const GMatrix m_drawnLabels.takeRow((GVec*)&m_labels[r]); // This case is only okay because we only use drawnLabels as a const GMatrix } // Train the learner with the drawn data m_pBag->models()[jobId]->m_pModel->train(m_drawnFeatures, m_drawnLabels); }
void update() { m_x = MAX(0, MIN(200, m_x + (int)m_rand.next(15) - 7)); m_y = MAX(0, MIN(500, m_y + (int)m_rand.next(15) - 7)); }
void resetSeed() { m_rand.setSeed(0); }
void shuffleBytes(unsigned char* pBuf, size_t len) { for(size_t i = len; i > 1; i--) std::swap(pBuf[(size_t)m_rand.next(i)], pBuf[i - 1]); }