PDA

View Full Version : مبتدی: مشکل در تولید وکتور با مقادیر رندوم



ehsan_faal
سه شنبه 02 تیر 1394, 00:57 صبح
سلام من از کتابخانه boost واسه تولید اعداد رندوم استفاده میکنم.
این یه بخشی از کدیه که قصد دارم کاملش کنم که یه وکتور دو بعدی رو مقداردهی میکنه(یعنی باید میکرد ولی نمیدونم مشکلش چیه).
ممنون میشم اشکال کارم رو بگید:
template <class T>
void InitializeRandomMatrix(vector<vector<T>> & C, int MinRange, int MaxRange)
{
int satr = C.size(), sotun = 0;
if (satr)
{
sotun = C[0].end() - C[0].begin();
}
else
return ;
boost::mt19937 gen(time(0));
boost::normal_distribution<T> nd(MinRange, MaxRange);
for (int r = 0; r < satr; r++)
{
for (int c = 0; c < sotun; C++‎‎‎‎)
C[r][c] = nd(gen);
}
}

و اینم main :

void main()
{
const int row = 3;
const int column = 3;
srand(time(0));
vector<vector<double>> Mat(row, vector<double>(column,0));

InitializeRandomMatrix(Mat, -13, 57);
DispArray(Mat);

cout << Maximum(Mat)<< endl;
cout << Minimum(Mat) << endl;

system("pause");
}

و اینم ارورش:


1>------ Build started: Project: Vector, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\users\ehsan\desktop\C++‎\vector\vector\source .cpp(92): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(2715): error C4996: 'std::_Fill_n': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++‎ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(2701) : see declaration of 'std::_Fill_n'
1> c:\boost\include\boost-1_58\boost\random\detail\polynomial.hpp(114) : see reference to function template instantiation '_OutIt std::fill_n<boost::random::detail::polynomial_ops::digit_t*,si ze_t,boost::random::detail::polynomial_ops::digit_ t>(_OutIt,_Diff,const _Ty &)' being compiled
1> with
1> [
1> _OutIt=boost::random::detail::polynomial_ops::digi t_t *
1> , _Diff=size_t
1> , _Ty=boost::random::detail::polynomial_ops::digit_t
1> ]
1>c:\users\ehsan\desktop\C++‎\vector\vector\source .cpp(21): warning C4244: 'argument' : conversion from 'time_t' to 'const uint32_t', possible loss of data
1> c:\users\ehsan\desktop\C++‎\vector\vector\source .cpp(95) : see reference to function template instantiation 'void InitializeRandomMatrix<double>(std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>> &,int,int)' being compiled
1> with
1> [
1> _Ty=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

ehsan_faal
سه شنبه 02 تیر 1394, 04:15 صبح
من مثالهای خود boost رو هم که مربط به تولید اعداد رندوم بود رو تست کردم اونا هم همین ارور رو میدن.
ولی قبلا که با regex این کتابخونه کار کرده بودم مشکلی وجود نداشت.
اصلا هم نمیدونم دنبال چی باید باشم چون هر چی گشتم موضوع مشابه با مشکل خودم رو تو نت ندیدم.

rahnema1
سه شنبه 02 تیر 1394, 14:47 عصر
سلام
کتابخانه استاندارد کلاسهای ایجاد اعداد تصادفی را داره که شبیه boost هست
اینجا که می خواهید توزیع نرمال استفاده کنید اولین پارامتر اون میانگین و دومی هم انحراف معیار توزیع هست نه حداقل و حداکثر
محدوده توزیع نرمال هم از منفی بی نهایت تا مثبت بی نهایت هست

#include <random>
//...
std::default_random_engine generator(time(NULL));
//std::default_random_engine generator( std::chrono::system_clock::now( ).time_since_epoch( ).count());
std::normal_distribution<> distribution(0.0, 1.0);
for (auto& row : C)
for(auto& element: row)
element = distribution(generator);