cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
How to pick a random number from an arra
How to pick a random number from an array
Oct 24, 2013 at 2:41am UTC
Bleedz129
(57)
Basically we have this 7 bit binary code, and i would like to pick out 3 numbers randomly from it, how do we do that ?
for example 1011101
1
2
3
4
5
int
Num[7];
for
(
int
i=6; i>=0 ; i--) {Num[i] = binary % 2; binary = binary/10;}
Oct 24, 2013 at 10:29am UTC
Bourgond Aries
(415)
Use the random number generators like std::mt19937 or std::rand(), documentation:
http://en.cppreference.com/w/cpp/numeric/random/rand
Oct 24, 2013 at 11:41am UTC
abhishekm71
(820)
You can also use
std::shuffle()
and pick the 1st three numbers after shuffling.
Oct 24, 2013 at 11:45am UTC
Bourgond Aries
(415)
I suppose that's correct, altho it would always take more CPU time to shuffle than to generate 3 random numbers and use those as indices.
Topic archived. No new replies allowed.