Войти в систему

Home
    - Создать дневник
    - Написать в дневник
       - Подробный режим

LJ.Rossia.org
    - Новости сайта
    - Общие настройки
    - Sitemap
    - Оплата
    - ljr-fif

Редактировать...
    - Настройки
    - Список друзей
    - Дневник
    - Картинки
    - Пароль
    - Вид дневника

Сообщества

Настроить S2

Помощь
    - Забыли пароль?
    - FAQ
    - Тех. поддержка



Пишет nancygold ([info]nancygold)
@ 2024-06-22 00:37:00

Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Настроение: amused

Have you ever seen this PRNG?
I've decided to look into the game's code out of curiosity.
Among the usual stuff, like EMS init, it seeds a random number generator.
Not a usual one, but with a 48-bit state.
Googled but could not find its origin.
Additionally, the game seeds that PRNG from the 40:6C at BDA.
Not something you usually see, since the Borland's C library provides a time access functions.

The game appears to be also using a custom EMS library, since it doesn't appear to match any of the free or shareware ones.

// ChatGPT claims it resembles the:
// 8-bit Maxim Integrated (formerly Dallas Semiconductor) one-wire CRC algorithm


//48-bit shift register
static uint8_t s[6]; //state

uint8_t rand8() { //1000:0730
  int i,c;
  uint8_t *src, *dst;
  //sum 1st, 4th and 5th bytes into the 0th byte
  c = s[1] + s[4] + 1;
  if (c&0x100) c = (c&0xFF) + 1;
  s[0] = (c + s[5])&0xFF;

  //shieft right by 8 bites
  src = s + 4;
  dst = s + 5;
  for (i = 5; i != 0; i = --i) *dst-- = *src--;

  return s[0];
}

void srand8(uint32_t seed) { //1000:0730
  int i;
  uint8_t *pseed;

  seed ^= 0x55555555;
  pseed = (uint8_t*)&seed;
  s[0] = 0;
  s[1] = pseed[0];
  s[2] = pseed[1];
  s[3] = pseed[2];
  s[4] = pseed[3];
  s[5] = 0;
  for (i = 0; i < 200; i++) rand8();
  return;
}



It is similar to the below code:
uint8_t rand48() {
    // Get the current state
    uint64_t s = get_state();

    // Apply bitwise transformations to update the state using only addition and shifting
    s = ((s << 13) | (s >> (48 - 13))) + 0xB;
    s &= ((1ULL << 48) - 1);

    // Store the updated state
    set_state(s);

    // Return the top 8 bits of the state
    return (s >> 40) & 0xFF;
}




(Читать комментарии)

Добавить комментарий:

Как:
Identity URL: 
имя пользователя:    
Вы должны предварительно войти в LiveJournal.com
 
E-mail для ответов: 
Вы сможете оставлять комментарии, даже если не введете e-mail.
Но вы не сможете получать уведомления об ответах на ваши комментарии!
Внимание: на указанный адрес будет выслано подтверждение.
Имя пользователя:
Пароль:
Тема:
HTML нельзя использовать в теме сообщения
Сообщение:



Обратите внимание! Этот пользователь включил опцию сохранения IP-адресов пишущих комментарии к его дневнику.