2
It’s actually fairly easy to write a PRNG. Here’s a short program that generates 100 pseudo- random numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <stdafx.h> #include <iostream> using namespace std; unsigned int PRNG() { // our initial starting seed is 5323 static unsigned int nSeed = 5323; // Take the current seed and generate a new value from it // Due to our use of large constants and overflow, it would be // very hard for someone to predict what the next number is // going to be from the previous one. nSeed = (8253729 * nSeed + 2396403); // Take the seed and return a value between 0 and 32767 return nSeed % 32767; } int main() { // Print 100 random numbers for (int nCount=0; nCount < 100; ++nCount) { cout << PRNG() << "\t"; // If we've printed 5 numbers, start a new column if ((nCount+1) % 5 == 0) cout << endl; }

c Plus Plus Name

Embed Size (px)

DESCRIPTION

c plus plus home work cpp

Citation preview

Its actually fairly easy to write a PRNG. Heres a short program that generates 100 pseudo-random numbers:12345678910111213141516171819202122232425262728293031#include #include using namespace std;unsigned int PRNG(){// our initial starting seed is 5323static unsigned int nSeed = 5323;// Take the current seed and generate a new value from it// Due to our use of large constants and overflow, it would be// very hard for someone to predict what the next number is// going to be from the previous one.nSeed = (8253729 * nSeed + 2396403);// Take the seed and return a value between 0 and 32767return nSeed % 32767;}int main(){// Print 100 random numbersfor (int nCount=0; nCount < 100; ++nCount){cout