TripleDecompositionProtocolInstance TripleDecompositionProtocolInstance::random ( int braid_rank , int conjugator_length , int element_length ) { int lowerIndex = braid_rank/3; int upperIndex = 2*lowerIndex; // A. Choose randomly the set of conjugators quadruple< Word , Word , Word , Word > C; C.first = Word::randomWord( braid_rank-1 , conjugator_length ); C.second = Word::randomWord( braid_rank-1 , conjugator_length ); C.third = Word::randomWord( braid_rank-1 , conjugator_length ); C.fourth = Word::randomWord( braid_rank-1 , conjugator_length ); // B. Choose randomly the private keys for both parties quintuple< Word , Word , Word , Word , Word > thePrivateKeyA; thePrivateKeyA.first = randomWord( 1 , braid_rank-1 , element_length ); // a1 thePrivateKeyA.fourth = -C.first * randomWord( 1 , lowerIndex-1 , element_length ) * C.first; // x1 thePrivateKeyA.second = -C.second * randomWord( 1 , lowerIndex-1 , element_length ) * C.second; // a2 thePrivateKeyA.fifth = -C.third * randomWord( 1 , upperIndex-1 , element_length ) * C.third; // x2 thePrivateKeyA.third = -C.fourth * randomWord( 1 , upperIndex-1 , element_length ) * C.fourth; // a3 quintuple< Word , Word , Word , Word , Word > thePrivateKeyB; thePrivateKeyB.first = -C.first * randomWord( lowerIndex+1 , braid_rank-1 , element_length ) * C.first; // b1 thePrivateKeyB.fourth = -C.second * randomWord( lowerIndex+1 , braid_rank-1 , element_length ) * C.second; // y1 thePrivateKeyB.second = -C.third * randomWord( upperIndex+1 , braid_rank-1 , element_length ) * C.third; // b2 thePrivateKeyB.fifth = -C.fourth * randomWord( upperIndex+1 , braid_rank-1 , element_length ) * C.fourth; // y2 thePrivateKeyB.third = randomWord( 1 , braid_rank-1 , element_length ); // b3 // return the result return TripleDecompositionProtocolInstance( braid_rank , C , thePrivateKeyA , thePrivateKeyB ); }
void benchmark_insert(int num_words, int word_len) { Cord::Ptr res = NULL; res = Cord::cord_from_char_star(randomWord(word_len)); for (int i = 1; i < num_words; i++) { const char *rand_string = randomWord(word_len); int insertion_pos = rand() % (res->length()); res = Cord::insert(res, rand_string, insertion_pos); } }
/* Picks the next word in the Markov chain by choosing a random number between zero and the total weights of all current's edges. It then iterates through current's edges by subtracting each edge's weight one at a time until random is less than zero. Whatever edge it's on then becomes the next word. This effectively chooses a word based on the probability that it follows the current word. This word becomes current. Preconditions: Some text has been added to the chain. Postconditions: Current* changes to a new word. */ Word * MarkovChain::nextWord(Word * current) { int totalWeight = 0; //stores the sum of all the weights of all the edges of Word* current for(int i = 0; i < current->edgeSize; i++) { totalWeight+= current->edges[i].occurrences; } std::random_device generator; std::uniform_int_distribution<int> randomweight (0,totalWeight-1); //Generate a random number between 0 and totalWeight-1 int random = randomweight(generator); Word * next = new Word; if(current->edges.size() > 0) { int j = 0; while(true) { random -= current->edges[j].occurrences; if(random < 0) { next = current->edges[j].next; break; } j++; } } else { next = randomWord(); } return next; }
GACPforORGSolverChromosome* GACPforORGSolverGene::randomChromosome( bool deg ) const { int cLen = 0; while( GAConjProblemForORGroupSolver::roulette( 1 , 3 ) ) ++cLen; Word c = randomWord( theGroup.numberOfGenerators( ), cLen ); return new GACPforORGSolverChromosome( c , deg ); }
/* GenerateString() makes a string of words, the length of which is passed in as an argument. This is printed to the console. It does this by selecting a random word from everything that it has read in. Every subsequent word is based off the probability that it would follow the previous word. Preconditions: Some text has been added to the Markov chain. Length is greater than zero. */ std::string MarkovChain::generateString(int length) { bool empty = true; for(int i = 0; i < hashTable->arraySize; i++) { std::cout << "[" << i << "]" << " : "; Word currentWord = hashTable->hashTable[i]; while(currentWord.next != NULL) { std::cout << " (" << currentWord.next->word << ")->"; currentWord = *(currentWord.next); if(empty) { empty = false; break; } } if(!empty) { break; } std::cout << std::endl; } if(empty) { return ""; } std::cout << length << std::endl; std::string output; Word * current = randomWord(); output.append(current->word); output.append(" "); for(int l = 0; l < length; l++) { if(current->edges.size() > 0) { if(isVerbose) { current->printWord(); } current = nextWord(current); output.append(current->word); output.append(" "); } } output.append("\b" "."); if(isVerbose) { std::cout << output << std::endl; } currentWord = current; return output; }