Пример #1
0
u32 sceKernelUtilsMt19937UInt(u32 ctx) {
	VERBOSE_LOG(HLE, "sceKernelUtilsMt19937UInt(%08x)", ctx);
	if (!Memory::IsValidAddress(ctx))
		return -1;
	MersenneTwister *mt = (MersenneTwister *)Memory::GetPointer(ctx);
	return mt->R32();
}
int test_getLogLikelihoodIncrease() {
    /* Disjoint union of two full graphs */
    Graph graph = *full(5) + *full(3);
    DegreeCorrectedUndirectedBlockmodel model =
        Blockmodel::create<DegreeCorrectedUndirectedBlockmodel>(&graph, 4);
    MersenneTwister rng;
    double predictedLogL, predictedDiff;

    /* Try four groups, do many random mutations */
    for (int i = 0; i < 8; i++)
        model.setType(i, i / 2);
    for (int i = 0; i < 10000; i++) {
        int from = rng.randint(8);
        PointMutation mutation(from, model.getType(from), rng.randint(4));
        while (mutation.from == mutation.to)
            mutation.to = rng.randint(4);

		predictedDiff = model.getLogLikelihoodIncrease(mutation);
        predictedLogL = model.getLogLikelihood() + predictedDiff;
        model.performMutation(mutation);

        if (!ALMOST_EQUALS(model.getLogLikelihood(), predictedLogL, 1e-3)) {
            std::cout << "Step #" << (i+1) << '\n'
					  << "Predicted diff = " << predictedDiff << '\n'
                      << "Predicted logL = " << predictedLogL << '\n'
					  << "Actual diff    = "
					  << model.getLogLikelihood() - (predictedLogL - predictedDiff) << '\n'
                      << "Actual logL    = " << model.getLogLikelihood() << '\n';
            return 1;
        }
    }

    return 0;
}
Пример #3
0
int main3()
{
    MersenneTwister x;

    for (int j=0; j<1000; j++) {
        printf("%10u ", x.random());
        if (j%8==7) printf("\n");
    }

    return 0;
}
int
main(int argc, char * argv[])
{
    // Create MonteCalroAsian object
    MersenneTwister clMersenneTwister;

    // Initialization
    if(clMersenneTwister.initialize() != SDK_SUCCESS)
    {
        return SDK_FAILURE;
    }

    // Parse command line options
    if(clMersenneTwister.sampleArgs->parseCommandLine(argc, argv))
    {
        return SDK_FAILURE;
    }

    // Validate the command line options
    if(clMersenneTwister.validateCommandLineOptions())
    {
        return SDK_EXPECTED_FAILURE;
    }

    if(clMersenneTwister.sampleArgs->isDumpBinaryEnabled())
    {
        return clMersenneTwister.genBinaryImage();
    }

    // Setup
    if(clMersenneTwister.setup() != SDK_SUCCESS)
    {
        return SDK_FAILURE;
    }

    // Run
    if(clMersenneTwister.run() != SDK_SUCCESS)
    {
        return SDK_FAILURE;
    }

    // Verifty
    if(clMersenneTwister.verifyResults() != SDK_SUCCESS)
    {
        return SDK_FAILURE;
    }

    // Cleanup resources created
    if(clMersenneTwister.cleanup() != SDK_SUCCESS)
    {
        return SDK_FAILURE;
    }

    // Print performance statistics
    clMersenneTwister.printStats();

    return SDK_SUCCESS;
}
Пример #5
0
int main(int argc, char const *argv[])
{
	MersenneTwister mt;

	ANN net(3, 0.1);

	vector<double> inputs;

	for (int i = 0; i < 10; ++i){

		vector<double> inputs;

		double i1 = boolize(1);
		double i2 = boolize(mt.random());
		double i3 = boolize(mt.random());

		double d = (i1 && i2 && i3) ? 0 : 1;

		inputs.push_back(i1);
		inputs.push_back(i2);
		inputs.push_back(i3);

		net.setInputs(inputs, d);

//		cout << net.getOutput() << endl;

		net.adjustWeights();
	}


	inputs.push_back(1);
	inputs.push_back(1);
	inputs.push_back(1);

	net.setInputs(inputs, 0);

	cout << net.getOutput() << endl;

	return 0;
}
Пример #6
0
void Route::fillweights(void)
{
    int i = 0, j = 0;
    
    for (i = 0; i < places; ++i) {
        for (j = 0; j < places; ++j) {
            if (i == j) {
                weights[i][j] = 100 * places;
            }
            else{
                weights[i][j] = mt.random() * places;
            }
        }
    }
}
Пример #7
0
void initMatrix (M &m)
{
   mt.init (42);

   for (unsigned d = 0; d < m.getDimension(); ++d)
   {
      for (unsigned r = 0; r < m.getM(); ++ r)
      {
         for (unsigned b = 0; b < m.getTotalPrec(); ++b)
         {
            m.setDigit (d, r, b, mt (m.getBase()));
         }
      }
   }
}
Пример #8
0
bool Route::moveTo(double E, double newE, int T)
{
    double P = 0;
    //double k = 0.1; // Needs to be some function of places number (DOES NOTHING)

    P = exp((E - newE)/(double)T);

    if (newE < E){
        return true;
    }
    else if (P > mt.random()){
        return true;
    }
    else{
        return false;
    }
}
Пример #9
0
bool verifyMatrix (M &m)
{
   mt.init (42);

   for (unsigned d = 0; d < m.getDimension(); ++d)
   {
      for (unsigned r = 0; r < m.getM(); ++ r)
      {
         for (unsigned b = 0; b < m.getTotalPrec(); ++b)
         {
            if (int (m.getDigit (d, r, b)) != mt (m.getBase()))
            {
               return false;
            }
         }
      }
   }

   return true;
}
Пример #10
0
void exercise21(){
/* Exercise 21 */
	std::array<long int, 30> expected_out =
			{
			1791095845, -12091157,	-59818354, -1431042742,
			491263,	1690620555,	1298508491,	-1144451193, 1637472845,
			1013994432, 396591248, 1703301249, 799981516, 1666063943,
			1484172013,

			2469588189546311528, 5937829314747939781, -1488664451840123274,
			8414607737543979063, -8983179180370611640, -4813816549494704131,
			-5143718920953096580, -3311530619265271089,	5943497028716478977,
			2456665931235054654, 5698940622110840090, -5231858944456961090,
			5552614544520314474, 6131760866643541936, 8415486058342034190
			};

	std::array<long int, 30> actual_out;

	MersenneTwister mt;

	// 32-bit test
	mt.set_bitsize(mt._32BIT);
	mt.srand_mt(1);

	int i;
	for(i = 0; i < 15; i++){
		actual_out[i] = mt.rand_mt();
	}

	// 64-bit test
	mt.set_bitsize(mt._64BIT);
	mt.srand_mt(1);

	for(; i < 30; i++){
		actual_out[i] = mt.rand_mt();
	}

	crypto_exercise_test(21, expected_out == actual_out);
}
Пример #11
0
vsx_rand::vsx_rand()
{
  MersenneTwister* mt = new MersenneTwister();
  mt->init_genrand(1);
  state = (void*)mt;
}
Пример #12
0
int main()
{
    int numValues = 6400000;
    double* values = new double [numValues];
    double value;
    MersenneTwister rng;
    for ( int i = 0; i < numValues; ++i )
    {
        values[i] = rng.rand_double_closed(-10000000, 10000000);
    }

    std::cout << "Testing big endian read/write" << std::endl;
    // write big
    std::ofstream bigFile( "/tmp/binaryBig", std::ios::binary );
    DwD bigWriter( bigFile, DwD::Binary, DwD::BigEndian );
    for ( int i = 0; i < numValues; ++i )
    {
        bigWriter.write( values + i );
    }
    bigFile.close();

    // read big and validate
    std::ifstream bigFileRead( "/tmp/binaryBig", std::ios::binary );
    DrD bigReader( bigFileRead, DrD::Binary, DrD::BigEndian );
    int count = 0;
    while ( bigReader.read( value ) )
    {
        assert( fabs( value - values[count] ) < 1e-8 );
        ++count;
    }
    assert( count == numValues );
    bigFileRead.close();
    std::cout << "  passed" << std::endl;
    
    std::cout << "Testing little endian read/write" << std::endl;
    // write little
    std::ofstream littleFile( "/tmp/binaryLittle", std::ios::binary );
    DwD littleWriter( littleFile, DwD::Binary, DwD::LittleEndian );
    for ( int i = 0; i < numValues; ++i )
    {
        littleWriter.write( values[i] );
    }
    littleFile.close();

    // read little and validate
    std::ifstream littleFileRead( "/tmp/binaryLittle", std::ios::binary );
    DrD littleReader( littleFileRead, DrD::Binary, DrD::LittleEndian );
    count = 0;
    while ( littleReader.read( value ) )
    {
        assert( fabs( value - values[count] ) < 1e-8 );
        ++count;
    }
    assert( count == numValues );
    littleFileRead.close();
    std::cout << "  passed" << std::endl;

    std::cout << "Testing text read/write" << std::endl;
    // write text
    std::ofstream textFile( "/tmp/text" );
    textFile.precision(16);
    textFile.setf( std::ios::scientific );
    DwD textWriter( textFile, DwD::Ascii );
    for ( int i = 0; i < 1000; ++i )
    {
        textWriter.write( values[i] );
        textWriter.linebreak();
    }
    textFile.close();

    // read text and validate
    std::ifstream textFileRead( "/tmp/text" );
    DrD textReader( textFileRead, DrD::Ascii );
    count = 0;
    double foo;
    while( textReader.read( value ) )
    {
        if ( fabs( value - values[ count ] ) >= 1e-8 )
        {
            std::cout << "value:" << value << " values[" 
                << count << "]:" << values[count] << std::endl;
        }
        assert( fabs( value - values[ count ] ) < 1e-8 );
        ++count;
    }
    assert( count == 1000 );
    textFileRead.close();
    std::cout << "  passed" << std::endl;
    delete [] values;
}
Пример #13
0
int _tmain(int argc, _TCHAR* argv[])
{

	MersenneTwister mt;
	//fm = new FaultMapper;
	mt.init_genrand64(57);

	cs = new CRITICAL_SECTION[NTHREADS];
	for (int i = 0; i < NTHREADS; i++)
		InitializeCriticalSection(&cs[i]);

	printf(

		"*** generating numbers ... ");
	bufInput =

		new UINT64[NITEMS_STATIC];
	int thread = 0;
	DWORD t = timeGetTime();
	//memset (bufSize, 0, sizeof(UINT64) * NTHREADS);
	for (SIZE_T i = 0; i < NITEMS_STATIC; i++)
	{

		bufInput[i] = mt.genrand64_int64();
	}

	printf(

		"done in %d ms\n", timeGetTime() - t);
	//getchar();
	jobQ =

		new queue<Job>[NTHREADS];
	// run the dispatcher
	int repeat = 10; // how many times to sort NITEMS
	int iterations = NITEMS / NITEMS_STATIC * repeat;
	UINT64 workloadItems = (UINT64)iterations * NITEMS_STATIC;
	UINT64 workloadBytes = workloadItems * 8;
	printf(

		"Sorting %.1fM items, %.1f GB, check if serialization is enabled\n", workloadItems / 1e6, workloadBytes / 1e9);
	PrintMemUsage();

	// usage due to main thread
	HANDLE threads[NTHREADS];
	int ids[NTHREADS];
	for (int i = 0; i < NTHREADS; i++)
	{

		ids[i] = i;

		if ((threads[i] = CreateThread(NULL, 0, BSthread, ids + i, 0, NULL)) == NULL)
			Fatal(

				"CreateThread", GetLastError());
	}

	// interleaved dispatcher
	for (int iter = 0; iter < iterations; iter++)
	{

		UINT64 off = 0;
		int thread = iter % NTHREADS; // TODO: round-robin through starting threads, so that each one eventually sorts entire array
		while (off < NITEMS_STATIC)
		{

			Job jb;
			jb.offset = off;

			jb.size =

				min(JOB_SIZE, NITEMS_STATIC - off);
			off += jb.size;

			// round-robin through all queues
			EnterCriticalSection(&cs[thread]);

			jobQ[thread].push(jb);

			LeaveCriticalSection(&cs[thread]);

			thread = (thread + 1) %

				NTHREADS;
		}

	}

	quit = true;
	// wait for finish
	for (int i = 0; i < NTHREADS; i++)
	{

		while (WaitForSingleObject(threads[i], 1000) == WAIT_TIMEOUT)
			//PrintMemUsage ();
			;

		// Fatal ("WaitForSingleObject", GetLastError());
		if (CloseHandle(threads[i]) == 0)
			Fatal(

				"CloseHandle", GetLastError());
	}

	//printf("speed = %.2f\n", speed);
	for (int i = 0; i < NTHREADS; i++)
		DeleteCriticalSection(&cs[i]);

	delete[] cs;
	delete[] jobQ;
	//for (int thread = 0; thread < NTHREADS; thread++)
	//delete sInput [thread];
	return 0;
}
Пример #14
0
	virtual unsigned char getNumber()
	{
		return static_cast<unsigned char>(twister.getNumber() &
				std::numeric_limits<unsigned char>::max());
	}
Пример #15
0
	virtual std::vector<unsigned int> getRandomDoubleWordVector(const size_t n)
	{
		return twister.getRandomDoubleWordVector(n);
	}
Пример #16
0
	virtual std::vector<unsigned short> getRandomWordVector(const size_t n)
	{
		return twister.getRandomWordVector(n);
	}
Пример #17
0
	virtual std::vector<unsigned char> getRandomByteVector(const size_t n)
	{
		return twister.getRandomByteVector(n);
	}