使用C/C++的QRandomGenerator类

176 篇文章 ¥59.90 ¥99.00
本文介绍了如何在C/C++中利用Qt框架的QRandomGenerator类生成随机数,包括随机整数、浮点数和字节序列。通过示例代码展示了如何使用bounded()和generate()函数进行不同类型的随机数生成。

使用C/C++的QRandomGenerator类

QRandomGenerator是Qt框架中提供的一个用于生成随机数的类。它可以用于生成各种类型的随机数,包括整数、浮点数和字节序列。在本文中,我们将详细介绍如何在C/C++中使用QRandomGenerator类来生成随机数。

首先,我们需要在代码中包含QRandomGenerator头文件:

#include <QRandomGenerator>

然后,我们可以使用QRandomGenerator类的静态函数来生成随机数。下面是几个常用的示例:

  1. 生成一个范围在0到99之间的随机整数:
int randomInt = QRandomGenerator::
make -C build 2 ↵ zheng@zheng-Dell-G16-7630 make: 进入目录“/home/zheng/test/Alliance-homework/qt-game/build” make[1]: 进入目录“/home/zheng/test/Alliance-homework/qt-game/build” make[2]: 进入目录“/home/zheng/test/Alliance-homework/qt-game/build” make[2]: 离开目录“/home/zheng/test/Alliance-homework/qt-game/build” [ 0%] Built target qt-game_autogen_timestamp_deps make[2]: 进入目录“/home/zheng/test/Alliance-homework/qt-game/build” make[2]: 离开目录“/home/zheng/test/Alliance-homework/qt-game/build” make[2]: 进入目录“/home/zheng/test/Alliance-homework/qt-game/build” [ 16%] Automatic MOC and UIC for target qt-game make[2]: 离开目录“/home/zheng/test/Alliance-homework/qt-game/build” [ 16%] Built target qt-game_autogen make[2]: 进入目录“/home/zheng/test/Alliance-homework/qt-game/build” make[2]: 离开目录“/home/zheng/test/Alliance-homework/qt-game/build” make[2]: 进入目录“/home/zheng/test/Alliance-homework/qt-game/build” [ 33%] Building CXX object CMakeFiles/qt-game.dir/qt-game_autogen/mocs_compilation.cpp.o [ 50%] Building CXX object CMakeFiles/qt-game.dir/main.cpp.o [ 66%] Building CXX object CMakeFiles/qt-game.dir/mainwindow.cpp.o In file included from /usr/include/c++/13/functional:49, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qcomparehelpers.h:27, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qcompare.h:632, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qnamespace.h:13, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qobjectdefs.h:12, from /home/zheng/Qt/6.8.3/gcc_64/include/QtGui/qwindowdefs.h:8, from /home/zheng/Qt/6.8.3/gcc_64/include/QtWidgets/qwidget.h:8, from /home/zheng/Qt/6.8.3/gcc_64/include/QtWidgets/qmainwindow.h:8, from /home/zheng/Qt/6.8.3/gcc_64/include/QtWidgets/QMainWindow:1, from /home/zheng/test/Alliance-homework/qt-game/mainwindow.h:4, from /home/zheng/test/Alliance-homework/qt-game/mainwindow.cpp:1: /usr/include/c++/13/bits/stl_function.h: In instantiation of ‘constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = QPoint]’: /usr/include/c++/13/bits/stl_tree.h:2534:33: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = QPoint; _Val = std::pair<const QPoint, QGraphicsRectItem*>; _KeyOfValue = std::_Select1st<std::pair<const QPoint, QGraphicsRectItem*> >; _Compare = std::less<QPoint>; _Alloc = std::allocator<std::pair<const QPoint, QGraphicsRectItem*> >; iterator = std::_Rb_tree<QPoint, std::pair<const QPoint, QGraphicsRectItem*>, std::_Select1st<std::pair<const QPoint, QGraphicsRectItem*> >, std::less<QPoint>, std::allocator<std::pair<const QPoint, QGraphicsRectItem*> > >::iterator]’ /usr/include/c++/13/bits/stl_map.h:1220:25: required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) [with _Key = QPoint; _Tp = QGraphicsRectItem*; _Compare = std::less<QPoint>; _Alloc = std::allocator<std::pair<const QPoint, QGraphicsRectItem*> >; iterator = std::_Rb_tree<QPoint, std::pair<const QPoint, QGraphicsRectItem*>, std::_Select1st<std::pair<const QPoint, QGraphicsRectItem*> >, std::less<QPoint>, std::allocator<std::pair<const QPoint, QGraphicsRectItem*> > >::iterator; key_type = QPoint]’ /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qmap.h:373:27: required from ‘T& QMap<Key, T>::operator[](const Key&) [with Key = QPoint; T = QGraphicsRectItem*]’ /home/zheng/test/Alliance-homework/qt-game/mainwindow.cpp:280:24: required from here /usr/include/c++/13/bits/stl_function.h:408:20: error: no match for ‘operator<’ (operand types are ‘const QPoint’ and ‘const QPoint’) 408 | { return __x < __y; } | ~~~~^~~~~ In file included from /usr/include/c++/13/utility:69, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qglobal.h:15, from /home/zheng/Qt/6.8.3/gcc_64/include/QtGui/qtguiglobal.h:7, from /home/zheng/Qt/6.8.3/gcc_64/include/QtWidgets/qtwidgetsglobal.h:7, from /home/zheng/Qt/6.8.3/gcc_64/include/QtWidgets/qmainwindow.h:7: /usr/include/c++/13/bits/stl_pair.h:835:5: note: candidate: ‘template<class _T1, class _T2> constexpr bool std::operator<(const pair<_T1, _T2>&, const pair<_T1, _T2>&)’ 835 | operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) | ^~~~~~~~ /usr/include/c++/13/bits/stl_pair.h:835:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::pair<_T1, _T2>’ 408 | { return __x < __y; } | ~~~~^~~~~ In file included from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qttypetraits.h:10, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qforeach.h:11, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qglobal.h:57: /usr/include/c++/13/optional:1254:5: note: candidate: ‘template<class _Tp, class _Up> constexpr std::__optional_lt_t<_Tp, _Up> std::operator<(const optional<_Tp>&, const optional<_Up>&)’ 1254 | operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs) | ^~~~~~~~ /usr/include/c++/13/optional:1254:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::optional<_Tp>’ 408 | { return __x < __y; } | ~~~~^~~~~ /usr/include/c++/13/optional:1322:5: note: candidate: ‘template<class _Tp> constexpr bool std::operator<(const optional<_Tp>&, nullopt_t)’ 1322 | operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept | ^~~~~~~~ /usr/include/c++/13/optional:1322:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::optional<_Tp>’ 408 | { return __x < __y; } | ~~~~^~~~~ /usr/include/c++/13/optional:1327:5: note: candidate: ‘template<class _Tp> constexpr bool std::operator<(nullopt_t, const optional<_Tp>&)’ 1327 | operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept | ^~~~~~~~ /usr/include/c++/13/optional:1327:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::optional<_Tp>’ 408 | { return __x < __y; } | ~~~~^~~~~ /usr/include/c++/13/optional:1388:5: note: candidate: ‘template<class _Tp, class _Up> constexpr std::__optional_lt_t<_Tp, _Up> std::operator<(const optional<_Tp>&, const _Up&)’ 1388 | operator<(const optional<_Tp>& __lhs, const _Up& __rhs) | ^~~~~~~~ /usr/include/c++/13/optional:1388:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::optional<_Tp>’ 408 | { return __x < __y; } | ~~~~^~~~~ /usr/include/c++/13/optional:1394:5: note: candidate: ‘template<class _Tp, class _Up> constexpr std::__optional_lt_t<_Up, _Tp> std::operator<(const _Up&, const optional<_Tp>&)’ 1394 | operator<(const _Up& __lhs, const optional<_Tp>& __rhs) | ^~~~~~~~ /usr/include/c++/13/optional:1394:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::optional<_Tp>’ 408 | { return __x < __y; } | ~~~~^~~~~ In file included from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qttypetraits.h:11: /usr/include/c++/13/tuple:1961:5: note: candidate: ‘template<class ... _TElements, class ... _UElements> constexpr bool std::operator<(const tuple<_UTypes ...>&, const tuple<_UTypes ...>&)’ 1961 | operator<(const tuple<_TElements...>& __t, | ^~~~~~~~ /usr/include/c++/13/tuple:1961:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::tuple<_UTypes ...>’ 408 | { return __x < __y; } | ~~~~^~~~~ In file included from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qttypetraits.h:14: /usr/include/c++/13/variant:1264:3: note: candidate: ‘template<class ... _Types> constexpr bool std::operator<(const variant<_Types ...>&, const variant<_Types ...>&)’ 1264 | _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/13/variant:1264:3: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::variant<_Types ...>’ 408 | { return __x < __y; } | ~~~~^~~~~ In file included from /usr/include/c++/13/bits/stl_algobase.h:67, from /usr/include/c++/13/bits/specfun.h:43, from /usr/include/c++/13/cmath:3699, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qnumeric.h:15, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qglobal.h:62: /usr/include/c++/13/bits/stl_iterator.h:455:5: note: candidate: ‘template<class _Iterator> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)’ 455 | operator<(const reverse_iterator<_Iterator>& __x, | ^~~~~~~~ /usr/include/c++/13/bits/stl_iterator.h:455:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::reverse_iterator<_Iterator>’ 408 | { return __x < __y; } | ~~~~^~~~~ /usr/include/c++/13/bits/stl_iterator.h:500:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)’ 500 | operator<(const reverse_iterator<_IteratorL>& __x, | ^~~~~~~~ /usr/include/c++/13/bits/stl_iterator.h:500:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::reverse_iterator<_Iterator>’ 408 | { return __x < __y; } | ~~~~^~~~~ /usr/include/c++/13/bits/stl_iterator.h:1705:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)’ 1705 | operator<(const move_iterator<_IteratorL>& __x, | ^~~~~~~~ /usr/include/c++/13/bits/stl_iterator.h:1705:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::move_iterator<_IteratorL>’ 408 | { return __x < __y; } | ~~~~^~~~~ /usr/include/c++/13/bits/stl_iterator.h:1770:5: note: candidate: ‘template<class _Iterator> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)’ 1770 | operator<(const move_iterator<_Iterator>& __x, | ^~~~~~~~ /usr/include/c++/13/bits/stl_iterator.h:1770:5: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const std::move_iterator<_IteratorL>’ 408 | { return __x < __y; } | ~~~~^~~~~ In file included from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qsharedpointer.h:12, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qdebug.h:18, from /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qvariant.h:12, from /home/zheng/Qt/6.8.3/gcc_64/include/QtGui/qaction.h:13, from /home/zheng/Qt/6.8.3/gcc_64/include/QtWidgets/qwidget.h:12: /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qsharedpointer_impl.h:816:24: note: candidate: ‘template<class T, class X> bool operator<(const QSharedPointer<T>&, const QSharedPointer<T>&)’ 816 | Q_INLINE_TEMPLATE bool operator<(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) | ^~~~~~~~ /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qsharedpointer_impl.h:816:24: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const QSharedPointer<T>’ 408 | { return __x < __y; } | ~~~~^~~~~ /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qsharedpointer_impl.h:822:24: note: candidate: ‘template<class T, class X> bool operator<(const QSharedPointer<T>&, X*)’ 822 | Q_INLINE_TEMPLATE bool operator<(const QSharedPointer<T> &ptr1, X *ptr2) | ^~~~~~~~ /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qsharedpointer_impl.h:822:24: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: ‘const QPoint’ is not derived from ‘const QSharedPointer<T>’ 408 | { return __x < __y; } | ~~~~^~~~~ /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qsharedpointer_impl.h:828:24: note: candidate: ‘template<class T, class X> bool operator<(T*, const QSharedPointer<T>&)’ 828 | Q_INLINE_TEMPLATE bool operator<(T *ptr1, const QSharedPointer<X> &ptr2) | ^~~~~~~~ /home/zheng/Qt/6.8.3/gcc_64/include/QtCore/qsharedpointer_impl.h:828:24: note: template argument deduction/substitution failed: /usr/include/c++/13/bits/stl_function.h:408:20: note: mismatched types ‘T*’ and ‘QPoint’ 408 | { return __x < __y; } | ~~~~^~~~~ /usr/include/c++/13/variant:1306:18: note: candidate: ‘constexpr bool std::operator<(monostate, monostate)’ 1306 | constexpr bool operator<(monostate, monostate) noexcept { return false; } | ^~~~~~~~ /usr/include/c++/13/variant:1306:28: note: no known conversion for argument 1 from ‘const QPoint’ to ‘std::monostate’ 1306 | constexpr bool operator<(monostate, monostate) noexcept { return false; } | ^~~~~~~~~ make[2]: *** [CMakeFiles/qt-game.dir/build.make:111:CMakeFiles/qt-game.dir/mainwindow.cpp.o] 错误 1 make[2]: 离开目录“/home/zheng/test/Alliance-homework/qt-game/build” make[1]: *** [CMakeFiles/Makefile2:86:CMakeFiles/qt-game.dir/all] 错误 2 make[1]: 离开目录“/home/zheng/test/Alliance-homework/qt-game/build” make: *** [Makefile:136:all] 错误 2 make: 离开目录“/home/zheng/test/Alliance-homework/qt-game/build”
10-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值