12#include <rtl/math.hxx>
17#include <document.hxx>
30const sal_Int64 DIST_UNIFORM = 0;
31const sal_Int64 DIST_NORMAL = 1;
32const sal_Int64 DIST_CAUCHY = 2;
33const sal_Int64 DIST_BERNOULLI = 3;
34const sal_Int64 DIST_BINOMIAL = 4;
35const sal_Int64 DIST_CHI_SQUARED = 5;
36const sal_Int64 DIST_GEOMETRIC = 6;
37const sal_Int64 DIST_NEGATIVE_BINOMIAL = 7;
38const sal_Int64 DIST_UNIFORM_INTEGER = 8;
39const sal_Int64 DIST_POISSON = 9;
42const sal_Int64 DIGITS = 4;
50 "modules/scalc/ui/randomnumbergenerator.ui",
51 "RandomNumberGeneratorDialog")
52 , mrViewData(rViewData)
53 , mrDoc(rViewData.GetDocument())
54 , mbDialogLostFocus(false)
55 , mxInputRangeText(m_xBuilder->weld_label(
"cell-range-label"))
56 , mxInputRangeEdit(new
formula::RefEdit(m_xBuilder->weld_entry(
"cell-range-edit")))
57 , mxInputRangeButton(new
formula::RefButton(m_xBuilder->weld_button(
"cell-range-button")))
58 , mxDistributionCombo(m_xBuilder->weld_combo_box(
"distribution-combo"))
59 , mxParameter1Text(m_xBuilder->weld_label(
"parameter1-label"))
60 , mxParameter1Value(m_xBuilder->weld_spin_button(
"parameter1-spin"))
61 , mxParameter2Text(m_xBuilder->weld_label(
"parameter2-label"))
62 , mxParameter2Value(m_xBuilder->weld_spin_button(
"parameter2-spin"))
63 , mxSeed(m_xBuilder->weld_spin_button(
"seed-spin"))
64 , mxEnableSeed(m_xBuilder->weld_check_button(
"enable-seed-check"))
65 , mxDecimalPlaces(m_xBuilder->weld_spin_button(
"decimal-places-spin"))
66 , mxEnableRounding(m_xBuilder->weld_check_button(
"enable-rounding-check"))
67 , mxButtonApply(m_xBuilder->weld_button(
"apply"))
68 , mxButtonOk(m_xBuilder->weld_button(
"ok"))
69 , mxButtonClose(m_xBuilder->weld_button(
"close"))
139 if ( rReferenceRange.
aStart != rReferenceRange.
aEnd )
158 sal_uInt32 seedValue;
162 seedValue =
mxSeed->get_value();
167 osl_getSystemTime(&
now);
168 seedValue =
now.Nanosec;
171 std::mt19937 seed(seedValue);
176 double parameter1 = parameterInteger1 /
static_cast<double>(
PRECISION);
177 double parameter2 = parameterInteger2 /
static_cast<double>(
PRECISION);
179 std::optional<sal_Int8> aDecimalPlaces;
189 std::uniform_real_distribution<> distribution(parameter1, parameter2);
190 auto rng = std::bind(distribution, seed);
194 case DIST_UNIFORM_INTEGER:
196 std::uniform_int_distribution<sal_Int64> distribution(parameterInteger1, parameterInteger2);
197 auto rng = std::bind(distribution, seed);
198 GenerateNumbers(rng, STR_DISTRIBUTION_UNIFORM_INTEGER, aDecimalPlaces);
203 std::normal_distribution<> distribution(parameter1, parameter2);
204 auto rng = std::bind(distribution, seed);
210 std::cauchy_distribution<> distribution(parameter1);
211 auto rng = std::bind(distribution, seed);
217 std::bernoulli_distribution distribution(parameter1);
218 auto rng = std::bind(distribution, seed);
224 std::binomial_distribution<> distribution(parameterInteger2, parameter1);
225 auto rng = std::bind(distribution, seed);
229 case DIST_CHI_SQUARED:
231 std::chi_squared_distribution<> distribution(parameter1);
232 auto rng = std::bind(distribution, seed);
238 std::geometric_distribution<> distribution(parameter1);
239 auto rng = std::bind(distribution, seed);
243 case DIST_NEGATIVE_BINOMIAL:
245 std::negative_binomial_distribution<> distribution(parameterInteger2, parameter1);
246 auto rng = std::bind(distribution, seed);
247 GenerateNumbers(rng, STR_DISTRIBUTION_NEGATIVE_BINOMIAL, aDecimalPlaces);
252 std::poisson_distribution<> distribution(parameter1);
253 auto rng = std::bind(distribution, seed);
263 OUString aUndo =
ScResId(STR_UNDO_DISTRIBUTION_TEMPLATE);
264 OUString aDistributionName =
ScResId(pDistributionStringId);
265 aUndo = aUndo.replaceAll(
"$(DISTRIBUTION)", aDistributionName);
278 std::vector<double> aVals;
279 aVals.reserve(nRowEnd - nRowStart + 1);
281 for (
SCROW nTab = nTabStart; nTab <= nTabEnd; ++nTab)
283 for (
SCCOL nCol = nColStart; nCol <= nColEnd; ++nCol)
288 for (
SCROW nRow = nRowStart; nRow <= nRowEnd; ++nRow)
292 aVals.push_back(rtl::math::round(randomGenerator(), *aDecimalPlaces));
294 aVals.push_back(randomGenerator());
308 ApplyClicked(*mxButtonApply);
309 CloseClicked(*mxButtonClose);
314 SelectGeneratorAndGenerateNumbers();
324 mxInputRangeEdit->SelectAll();
329 mxInputRangeEdit->SelectAll();
334 mbDialogLostFocus = !
m_xDialog->has_toplevel_focus();
339 mbDialogLostFocus = !
m_xDialog->has_toplevel_focus();
345 bool bValid = ParseWithNames( aRangeList, mxInputRangeEdit->GetText(), mrDoc);
346 const ScRange* pRange = (bValid && aRangeList.
size() == 1) ? &aRangeList[0] :
nullptr;
349 maInputRange = *pRange;
350 mxButtonApply->set_sensitive(
true);
351 mxButtonOk->set_sensitive(
true);
353 mxInputRangeEdit->StartUpdateData();
358 mxButtonApply->set_sensitive(
false);
359 mxButtonOk->set_sensitive(
false);
365 sal_Int64 aSelectedId = mxDistributionCombo->get_active_id().toInt64();
366 if (aSelectedId == DIST_UNIFORM ||
367 aSelectedId == DIST_UNIFORM_INTEGER)
369 sal_Int64
min = mxParameter1Value->get_value();
370 sal_Int64
max = mxParameter2Value->get_value();
373 mxParameter2Value->set_value(
min);
380 sal_Int64 aSelectedId = mxDistributionCombo->get_active_id().toInt64();
381 if (aSelectedId == DIST_UNIFORM ||
382 aSelectedId == DIST_UNIFORM_INTEGER)
384 sal_Int64
min = mxParameter1Value->get_value();
385 sal_Int64
max = mxParameter2Value->get_value();
388 mxParameter1Value->set_value(
max);
395 mxSeed->set_sensitive(mxEnableSeed->get_active());
396 mxDecimalPlaces->set_sensitive(mxEnableRounding->get_active());
401 sal_Int64 aSelectedId = mxDistributionCombo->get_active_id().toInt64();
406 mxParameter1Value->set_digits(DIGITS);
409 mxParameter2Value->set_digits(DIGITS);
416 mxParameter1Text->set_label(
ScResId(STR_RNG_PARAMETER_MINIMUM));
417 mxParameter2Text->set_label(
ScResId(STR_RNG_PARAMETER_MAXIMUM));
418 mxParameter2Text->show();
419 mxParameter2Value->show();
422 case DIST_UNIFORM_INTEGER:
424 mxParameter1Text->set_label(
ScResId(STR_RNG_PARAMETER_MINIMUM));
425 mxParameter1Value->set_digits(0);
426 mxParameter1Value->set_increments(1, 10);
428 mxParameter2Text->set_label(
ScResId(STR_RNG_PARAMETER_MAXIMUM));
429 mxParameter2Value->set_digits(0);
430 mxParameter2Value->set_increments(1, 10);
432 mxParameter2Text->show();
433 mxParameter2Value->show();
438 mxParameter1Text->set_label(
ScResId(STR_RNG_PARAMETER_MEAN));
439 mxParameter2Text->set_label(
ScResId(STR_RNG_PARAMETER_STANDARD_DEVIATION));
440 mxParameter2Text->show();
441 mxParameter2Value->show();
446 mxParameter1Text->set_label(
ScResId(STR_RNG_PARAMETER_STANDARD_MEDIAN));
447 mxParameter2Text->set_label(
ScResId(STR_RNG_PARAMETER_STANDARD_SIGMA));
448 mxParameter2Text->show();
449 mxParameter2Value->show();
455 mxParameter1Text->set_label(
ScResId(STR_RNG_PARAMETER_STANDARD_PROBABILITY));
456 mxParameter1Value->set_range(0,
PRECISION);
457 mxParameter1Value->set_increments(1000, 10000);
459 mxParameter2Text->hide();
460 mxParameter2Value->hide();
464 case DIST_NEGATIVE_BINOMIAL:
466 mxParameter1Text->set_label(
ScResId(STR_RNG_PARAMETER_STANDARD_PROBABILITY));
467 mxParameter1Value->set_range(0,
PRECISION);
468 mxParameter1Value->set_increments(1000, 10000);
470 mxParameter2Text->set_label(
ScResId(STR_RNG_PARAMETER_STANDARD_NUMBER_OF_TRIALS));
471 mxParameter2Value->set_digits(0);
472 mxParameter2Value->set_increments(1, 10);
473 mxParameter2Value->set_min(0);
475 mxParameter2Text->show();
476 mxParameter2Value->show();
479 case DIST_CHI_SQUARED:
481 mxParameter1Text->set_label(
ScResId(STR_RNG_PARAMETER_STANDARD_NU_VALUE));
483 mxParameter2Text->hide();
484 mxParameter2Value->hide();
489 mxParameter1Text->set_label(
ScResId(STR_RNG_PARAMETER_MEAN));
491 mxParameter1Value->set_increments(1000, 10000);
492 mxParameter1Value->set_min(1000);
493 mxParameter2Text->hide();
494 mxParameter2Value->hide();
IMPL_LINK_NOARG(ScRandomNumberGeneratorDialog, OkClicked, weld::Button &, void)
Reference< XExecutableDialog > m_xDialog
static sal_uInt16 GetChildWindowId()
void SetValueCells(const ScAddress &rPos, const std::vector< double > &aVals, bool bInteraction)
void PostPaint(SCCOL nStartCol, SCROW nStartRow, SCTAB nStartTab, SCCOL nEndCol, SCROW nEndRow, SCTAB nEndTab, PaintPartFlags nPart, sal_uInt16 nExtFlags=0)
virtual SfxUndoManager * GetUndoManager() override
SC_DLLPUBLIC formula::FormulaGrammar::AddressConvention GetAddressConvention() const
std::unique_ptr< weld::CheckButton > mxEnableSeed
std::unique_ptr< weld::Button > mxButtonApply
virtual void SetReference(const ScRange &rRef, ScDocument &rDoc) override
std::unique_ptr< weld::CheckButton > mxEnableRounding
std::unique_ptr< weld::ComboBox > mxDistributionCombo
std::unique_ptr< weld::Label > mxInputRangeText
std::unique_ptr< weld::Button > mxButtonOk
std::unique_ptr< weld::SpinButton > mxParameter2Value
std::unique_ptr< weld::Button > mxButtonClose
std::unique_ptr< weld::SpinButton > mxDecimalPlaces
std::unique_ptr< weld::SpinButton > mxParameter1Value
void GetRangeFromSelection()
std::unique_ptr< formula::RefButton > mxInputRangeButton
virtual void Close() override
void SelectGeneratorAndGenerateNumbers()
virtual void SetActive() override
std::unique_ptr< formula::RefEdit > mxInputRangeEdit
ScRandomNumberGeneratorDialog(SfxBindings *pB, SfxChildWindow *pCW, weld::Window *pParent, ScViewData &rViewData)
void GenerateNumbers(RNG &randomGenerator, TranslateId pDistributionStringId, const std::optional< sal_Int8 > aDecimalPlaces)
virtual ~ScRandomNumberGeneratorDialog() override
std::unique_ptr< weld::SpinButton > mxSeed
OUString Format(const ScDocument &rDocument, ScRefFlags nFlags=ScRefFlags::ZERO, const ScAddress::Details &rDetails=ScAddress::detailsOOOa1, bool bFullAddressNotation=false) const
Returns string with formatted cell range from aStart to aEnd, according to provided address conventio...
virtual void RefInputStart(formula::RefEdit *pEdit, formula::RefButton *pButton=nullptr) override
virtual void RefInputDone(bool bForced=false) override
bool DoClose(sal_uInt16 nId)
ScDocShell * GetDocShell() const
ScTabViewShell * GetViewShell() const
ScMarkType GetSimpleArea(SCCOL &rStartCol, SCROW &rStartRow, SCTAB &rStartTab, SCCOL &rEndCol, SCROW &rEndRow, SCTAB &rEndTab) const
virtual void EnterListAction(const OUString &rComment, const OUString &rRepeatComment, sal_uInt16 nId, ViewShellId nViewShellId)
ViewShellId GetViewShellId() const override
#define LINK(Instance, Class, Member)
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
OUString ScResId(TranslateId aId)