Вопрос

I would like to ask, how exactly is used memento in pseudorandom number generator? I have high level knowledge of pseudorandom number generator but I don't see there any memento (even I read it's there). Thank you for your answer so much :)

Это было полезно?

Решение

I believe you are talking about design pattern memento. If so, then in my opinion memento is used as the inner state of random number generator. First you create random number generator with particular seed (that is its state) and then you use this seed during the next random number generation. So using a standard memento terminology:

  • originator is random number generator,
  • caretaker is a caller who retrieves numbers by using random number generator,
  • memento is a state of random number generator based on which the next random value is created

Standard rand() in C++ does not support retrieving its state so the only restore operation is simply to store the seed you are setting via srand() at the beginning and then us it to restore generator to initial state.

However, you can reimplement it so it supports state querying and then subsequent restore to any, not only initial, state. For way how to achieve that see this SO answer. Also it is mentioned in the same thread that new c++11 random number generators offer this functionality by default.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top