Skip to content

Fix: Always return false when likelihood is zero#315

Merged
FlorianKoerner merged 1 commit intodicebear:mainfrom
ralacerda:fix-prng
May 14, 2023
Merged

Fix: Always return false when likelihood is zero#315
FlorianKoerner merged 1 commit intodicebear:mainfrom
ralacerda:fix-prng

Conversation

@ralacerda
Copy link
Contributor

I noticed that sometimes, even when the probability of an option was zero, it would show up from time to time.

The bug was coming from prng.bool(), since integer(0, 100) would return a random value including 0 or 100. This means that even when the likelihood was 0, there was a 1 in 101 chance of returning true.

My PR fix this by changing the integer call from integer(0, 100) to integer(1, 100), this way, If likelihood is 0, it'll always return false.
This also aligns the likelihood with the probability, meaning that a likelihood of 1 has a 1 in 100 chance, instead of 1 in 101 (since 0 was included).

@FlorianKoerner FlorianKoerner merged commit 598b7af into dicebear:main May 14, 2023
@FlorianKoerner
Copy link
Member

Thanks for the fix :)

@FlorianKoerner
Copy link
Member

Because this fix changes the output of multiple avatars, it will not be released until the next major release.

As a small workaround, you could also set the corresponding option to an empty array. Here is an example:

JS-Library

import { createAvatar } from '@dicebear/core';
import { adventurer } from '@dicebear/collection';

const avatar = createAvatar(adventurer, {
  "features": [],
  "featuresProbability": 0
});

const svg = avatar.toString();

HTTP-API

https://api.dicebear.com/6.x/adventurer/svg?features[]&featuresProbability=0

CLI

dicebear adventurer . --features --featuresProbability 0

@ralacerda ralacerda deleted the fix-prng branch May 14, 2023 14:06
FlorianKoerner added a commit that referenced this pull request May 18, 2023
@FlorianKoerner
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants