Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ;-------------------------------; Random Number Generator
- GetRand: ; By: 1024
- PHY ; Comments By: Fakescaper
- LDY #$01 ;
- JSR .Maths ;
- DEY ;
- JSR .Maths ;
- PLY ;
- RTL ;
- .Maths LDA $148B ;
- ASL #2 ;
- SEC ;
- ADC $148B ;
- STA $148B ;
- ASL $148C ;
- LDA #$20 ;
- BIT $148C ;
- BCC .Label1 ; If any of the ADC or ASL cleared carry,
- BEQ .Label2 ; reverse the results of the BIT test
- BNE .Label3 ;
- .Label1 BNE .Label2 ;
- .Label3 ;
- INC $148C ; when determining whether or not we INC
- .Label2 ;
- LDA $148C ;
- EOR $148B ;
- STA $148D,Y ; Output gets stored in this RAM Address, 16 bit result.
- RTS ; You might want to add an ADC $13 or SBC $94 before this STA for extra "random"
- ;-------------------------------; (or you can just do it after calling this routine)
- */
- /*
- Below is a rewritten version that outputs the same numbers pretty much
- A random routine that lets you specify how many bytes of entropy to produce.
- */
- #include <iostream>
- #include <iomanip>
- #include <ctime> // For the time function
- using namespace std;
- // Process setting/clearing the carry flag
- void Process_C(int &A, bool &PF_C)
- {
- PF_C = false;
- while(A < 0 || A > 255){PF_C = true; A &= 0xFF;}
- }
- //[0, ((256^bytes)-1)]
- unsigned long int GetRand(int bytes, int &ADR_148B, int &ADR_148C, bool &PF_C)
- {
- unsigned long int out = 0;
- do
- {
- int temp = ADR_148B;
- temp = temp << 2;
- temp += ADR_148B + 1;
- temp &= 0xFF;
- ADR_148B = temp;
- ADR_148C = ADR_148C << 1;
- Process_C(ADR_148C, PF_C);
- if ( (!PF_C) ^ (0x20 & ADR_148C) )
- {
- if (++ADR_148C > 255)
- {
- ADR_148C = 0;
- }
- }
- temp = ADR_148C^ADR_148B;
- out |= temp;
- while(bytes > 1 && (out & 0xFF))
- {
- out = out << 8;
- }
- bytes--;
- }
- while(bytes > 0);
- return out;
- }
- // How many digits the decimal values of bytes will be
- int wid[] = {
- 0, 3, 5, 8, 10, 13, 15, 17
- };
- int main()
- {
- // The Carry Processor Flag
- bool PF_C;
- int ADR_148B;
- int ADR_148C;
- ADR_148B = (ADR_148B + time(0)) & 0xFF;
- ADR_148C = (ADR_148C + time(0)) & 0xFF;
- unsigned long int temp;
- for(int i = 1; i <= 5; i++)
- {
- for(int i = 1; i <= 4; i++)
- {
- temp = GetRand(i, ADR_148B, ADR_148C, PF_C); // i bytes of entropy from 1-4 (8-bit, 32-bit, 512-bit, 4096-bit)
- cout << "0x" << setfill('0') << setw(i*2) << uppercase << hex << temp << " (" << setfill(' ') << setw(wid[i]) << dec << temp << ") - ";
- }
- cout << "\n";
- //temp = GetRand(2, ADR_148B, ADR_148C, PF_C); 2 bytes of entropy
- //cout << "\n" << "0x" << uppercase << hex << temp << " (" << dec << temp << ")\n\n";
- }
- cout << "\n";
- long double avg = 0;
- int cnt = 0;
- for(int i = 1; i <= 14; i++)
- {
- for(int i = 1; i <= 6; i++)
- {
- temp = GetRand(1, ADR_148B, ADR_148C, PF_C);
- avg += temp;
- cnt++;
- cout << "0x" << setfill('0') << setw(2) << uppercase << hex << temp << " (" << setfill(' ') << setw(3) << dec << temp << ") - ";
- }
- cout << "\n";
- }
- cout << "The average of " << cnt << " random values was: " << avg/cnt << " for 1 byte of entropy (0-255).";
- cout << "\nWhich is a difference of " << (avg/cnt)-127.5 << "\nFrom the expected average of 127.5";
- /*
- 2 bytes: 32767.5
- 3 bytes: 8388607.5
- 4 bytes: 2147483647.5
- */
- cout << "\n\nPress Enter to Exit. ";
- cin.ignore();
- //cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement