Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code has been improvised to generate fast response #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iamananya
Copy link
Owner

@GuiltyMorishita the code have been improved for a fast response using hash tables.
image

The implementation details are:

  • The GenerateCharacterPool function creates a hash table (probability map) to store the character probabilities. The character ID is used as the key in the hash table, and the probability is the associated value.
  • The FindCharacterByID function iterates across the probability map and returns the character for each ID. The function directly retrieves the character using the ID from the hash table rather than scanning through the complete character slice for each character.
  • The character pool is drawn at random using the DrawCharacter function

@iamananya
Copy link
Owner Author

And for time=100000 the response time is:
image

@iamananya
Copy link
Owner Author

iamananya commented Jun 1, 2023

Also the results for times=20 are:

R=16/20 which is 80% probability

SR=3/20 which is 15% probability

SSR=1/20 which is 5% probability
The results are according to the probability mentioned in the code. This proves the correctness of the code.

{
"results": [
{
"characterID": "Character-46",
"name": "Xiangling",
"rarity": "R"
},
{
"characterID": "Character-18",
"name": "Venti",
"rarity": "R"
},
{
"characterID": "Character-28",
"name": "Arataki Itto",
"rarity": "R"
},
{
"characterID": "Character-15",
"name": "Mona",
"rarity": "SR"
},
{
"characterID": "Character-13",
"name": "Kazuha",
"rarity": "SR"
},
{
"characterID": "Character-11",
"name": "Hu Tao",
"rarity": "R"
},
{
"characterID": "Character-33",
"name": "Zhongli",
"rarity": "SR"
},
{
"characterID": "Character-11",
"name": "Hu Tao",
"rarity": "R"
},
{
"characterID": "Character-14",
"name": "Kokomi",
"rarity": "R"
},
{
"characterID": "Character-27",
"name": "Gorou",
"rarity": "R"
},
{
"characterID": "Character-46",
"name": "Xiangling",
"rarity": "R"
},
{
"characterID": "Character-14",
"name": "Kokomi",
"rarity": "R"
},
{
"characterID": "Character-46",
"name": "Xiangling",
"rarity": "R"
},
{
"characterID": "Character-45",
"name": "Barbara",
"rarity": "R"
},
{
"characterID": "Character-7",
"name": "Water Nymph",
"rarity": "R"
},
{
"characterID": "Character-11",
"name": "Hu Tao",
"rarity": "R"
},
{
"characterID": "Character-38",
"name": "Raiden Shogun",
"rarity": "SSR"
},
{
"characterID": "Character-7",
"name": "Water Nymph",
"rarity": "R"
},
{
"characterID": "Character-8",
"name": "Ayaka",
"rarity": "R"
},
{
"characterID": "Character-46",
"name": "Xiangling",
"rarity": "R"
}
]
}

@GuiltyMorishita
Copy link
Collaborator

I confirm that it has been more efficient.
However, it appears that the user's character is not stored in the database, is that correct?

@GuiltyMorishita
Copy link
Collaborator

GuiltyMorishita commented Jun 1, 2023

@iamananya The proof is not enough.
The number of times is too small for 20 times, use the data of 10000 times.
You only looked at rarity, right? You also need to check if characters of the same rarity are draw at the same rate.

@iamananya
Copy link
Owner Author

@iamananya The proof is not enough. The number of times is too small for 20 times, use the data of 10000 times. You only looked at rarity, right? You also need to check if characters of the same rarity are draw at the same rate.

Yes I only checked the rarity. I will show you the proof of 10000 times data as well.
I didn't understand the meaning of character of same rarity are drawn at same rate. Can you please explain @GuiltyMorishita

@iamananya
Copy link
Owner Author

I confirm that it has been more efficient. However, it appears that the user's character is not stored in the database, is that correct?

Yes they are not stored 🥲.

@iamananya
Copy link
Owner Author

I confirm that it has been more efficient. However, it appears that the user's character is not stored in the database, is that correct?

I have added in the latest commit @GuiltyMorishita .

@GuiltyMorishita
Copy link
Collaborator

GuiltyMorishita commented Jun 2, 2023

I didn't understand the meaning of character of same rarity are drawn at same rate. Can you please explain

@iamananya

I think it is the following logic.

  • There are two S-ranked characters. (Character A, B).
  • The draw rate of S-ranked characters is 10%.
  • Therefore, the draw rate of character A is 5%.

Therefore, I want you to prove that each character is drawn according to the assumed probability.
And I want to make sure that it is not the case that only certain characters are draw more.

return nil
}

func createBatch(tx *gorm.DB, batch []*UserCharacter) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function name makes it difficult to understand what is being done from the outside.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Okay I will change the function name.

i++
}
// Batch insert user characters into the database
if err := models.CreateUserCharacterBatch(userCharacters); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please name it in a way that makes it clear that you are not creating a batch, but rather creating characters with batch.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Okay I meant create user's character batch hence I named it that way.However I will change it if it doesn't make understandable.

@@ -141,59 +143,46 @@ func HandleGachaDraw(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
characterPool := generateCharacterPool(characters)
characterPool := GenerateCharacterPool(characters)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the reason for capitalization?

Copy link
Owner Author

Choose a reason for hiding this comment

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

There is no reason, I just liked it that way 😄

Copy link
Collaborator

Choose a reason for hiding this comment

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

@iamananya
In Golang, case has meaning.
You need to look up the Golang specs.

Copy link
Owner Author

Choose a reason for hiding this comment

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

@GuiltyMorishita Since this function is not exported, I will make it lowercase.

@iamananya
Copy link
Owner Author

@GuiltyMorishita I have added all the changes suggested by you . Since I needed to prove the correctness. and I was getting some vague results in the previous implementation. Hence I have modified the code as well.

changes for multiple gacha also added here
@iamananya
Copy link
Owner Author

This PR fixes issue #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants