Is there any function or something like that by which I can create totally random strings or numbers?
相关问题
- QML: Cannot read property 'xxx' of undefin
- QTextEdit.find() doesn't work in Python
- Unity - Get Random Color at Spawning
- QT Layouts, how to make widgets in horizontal layo
- QT Layouts, how to make widgets in horizontal layo
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Is there a non-java, cross platform way to launch
- why 48 bit seed in util Random class?
- How to get a settings storage path in a cross-plat
- How to set the font size of the label on pushbutto
- Why doesn't valgrind detect a memory leak in m
- QTreeView remove decoration/expand button for all
returns a random number
randomValue
with 0 <=randomValue
<number
.qrand()
is declared in QtGlobal which is #included by many other Qt files.converts an integer to QString.
This is not a very good method to generate random numbers within a given range. (In fact it's very very bad for most generators )
You are assuming that the low-order bits from the generator are uniformly distributed. This is not the case with most generators. In most generators the randomness occurs in the high order bits.
By using the remainder after divisions you are in effect throwing out the randomness.
You should scale using multiplication and division. Not using the modulo operator. eg
If
generator_output
is in[0, generator_maximum]
,my_number
will be in[start_required , start_required + range_required]
.You can create random numbers using qrand. If you need strings, you can convert the int to string. You could also check the QUuid class, which generates Universally Unique Identifiers. Those are not 'totally random', but they are unique.
Here is the good answer using qrand(). The solution below uses QUuid, as already was suggested above, to generate random and unique ids (they are all
hex
numbers):Output
Use QUuid
The following example generates alphabetic strings with capital letters from A to Z and length = len.