Skip to content

Commit

Permalink
Simplify and clean code related to class TRand
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Nov 25, 2024
1 parent 9f17a3f commit c1bb752
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/ccstruct/imagedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ void DocumentData::Shuffle() {
TRand random;
// Different documents get shuffled differently, but the same for the same
// name.
random.set_seed(document_name_.c_str());
std::hash<std::string> hasher;
random.set_seed(static_cast<uint64_t>(hasher(document_name_)));
int num_pages = pages_.size();
// Execute one random swap for each page in the document.
for (int i = 0; i < num_pages; ++i) {
Expand Down
7 changes: 0 additions & 7 deletions src/ccutil/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
#include <climits> // for INT_MIN, INT_MAX
#include <cmath> // std::isfinite
#include <cstdio>
#include <cstring>
#include <algorithm> // for std::find
#include <functional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -77,11 +75,6 @@ class TRand {
void set_seed(uint64_t seed) {
seed_ = seed;
}
// Sets the seed using a hash of a string.
void set_seed(const std::string &str) {
std::hash<std::string> hasher;
set_seed(static_cast<uint64_t>(hasher(str)));
}

// Returns an integer in the range 0 to INT32_MAX.
int32_t IntRand() {
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/lstmrecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class TESS_API LSTMRecognizer {
protected:
// Sets the random seed from the sample_iteration_;
void SetRandomSeed() {
int64_t seed = static_cast<int64_t>(sample_iteration_) * 0x10000001;
int64_t seed = sample_iteration_ * 0x10000001LL;
randomizer_.set_seed(seed);
randomizer_.IntRand();
}
Expand Down

4 comments on commit c1bb752

@egorpugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add time(0) multiplier?

@stweil
Copy link
Member Author

@stweil stweil commented on c1bb752 Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get random crashes? Nice idea. :-)

Seriously, no.

@egorpugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.
Why hide problems instead of fixing them?

@egorpugin
Copy link
Contributor

@egorpugin egorpugin commented on c1bb752 Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is clear that people may get more crashes even on this number series.
It's not possible to fix all at once selecting 'the best' and 'crash-free' series.

Please sign in to comment.