| 448 | } |
| 449 | |
| 450 | void RandomValuesDialog::generate() { |
| 451 | Q_ASSERT(m_spreadsheet); |
| 452 | |
| 453 | WAIT_CURSOR; |
| 454 | const int rows = m_spreadsheet->rowCount(); |
| 455 | QVector<double> data; |
| 456 | QVector<int> data_int; |
| 457 | QVector<qint64> data_bigint; |
| 458 | try { |
| 459 | data.resize(rows); |
| 460 | data_int.resize(rows); |
| 461 | data_bigint.resize(rows); |
| 462 | } catch (std::bad_alloc&) { |
| 463 | RESET_CURSOR; |
| 464 | QMessageBox::critical(this, i18n("Failed to allocate memory"), i18n("Not enough memory to perform this operation.")); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | // create a generator chosen by the environment variable GSL_RNG_TYPE |
| 469 | gsl_rng_env_setup(); |
| 470 | const gsl_rng_type* T = gsl_rng_default; |
| 471 | gsl_rng* r = gsl_rng_alloc(T); |
| 472 | |
| 473 | ulong seed; |
| 474 | if (!ui.leSeed->text().isEmpty()) { |
| 475 | bool ok; |
| 476 | seed = ui.leSeed->text().toULong(&ok); |
| 477 | if (!ok) |
| 478 | seed = QDateTime::currentMSecsSinceEpoch(); |
| 479 | } else |
| 480 | seed = QDateTime::currentMSecsSinceEpoch(); |
| 481 | |
| 482 | gsl_rng_set(r, seed); |
| 483 | |
| 484 | m_spreadsheet->beginMacro( |
| 485 | i18np("%1: fill column with non-uniform random numbers", "%1: fill columns with non-uniform random numbers", m_spreadsheet->name(), m_columns.size())); |
| 486 | |
| 487 | for (auto* col : m_columns) { |
| 488 | col->setSuppressDataChangedSignal(true); |
| 489 | col->clearFormula(); // clear the potentially available column formula |
| 490 | } |
| 491 | |
| 492 | const auto dist = (nsl_sf_stats_distribution)ui.cbDistribution->currentData().toInt(); |
| 493 | DEBUG(Q_FUNC_INFO << ", random number distribution: " << nsl_sf_stats_distribution_name[dist]); |
| 494 | |
| 495 | Column::RandomValuesData randomValuesData; |
| 496 | randomValuesData.available = true; |
| 497 | randomValuesData.distribution = dist; |
| 498 | SET_DOUBLE_FROM_LE(randomValuesData.parameter1, ui.leParameter1) |
| 499 | SET_DOUBLE_FROM_LE(randomValuesData.parameter2, ui.leParameter2) |
| 500 | SET_DOUBLE_FROM_LE(randomValuesData.parameter3, ui.leParameter3) |
| 501 | if (!ui.leSeed->text().isEmpty()) |
| 502 | randomValuesData.seed = seed; |
| 503 | |
| 504 | switch (dist) { |
| 505 | case nsl_sf_stats_gaussian: { |
| 506 | double mu{0.0}, sigma{1.0}; |
| 507 | SET_DOUBLE_FROM_LE(mu, ui.leParameter1) |
no test coverage detected