cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
randomly selecting already inputed numbe
randomly selecting already inputed number
Mar 12, 2014 at 8:06am UTC
chofs
(49)
hey guys just want to know if i had a stream of numbers(1,1,2,5,8) and want to select at random one of the 5...hw do i go about doing dat using rand() or srand()
Mar 12, 2014 at 8:42am UTC
keskiverto
(10366)
http://www.cplusplus.com/reference/cstdlib/rand/
They can give you a number in range from 0 to 4. Where did you store the numbers?
Mar 12, 2014 at 8:57am UTC
chofs
(49)
array
Mar 12, 2014 at 9:01am UTC
chofs
(49)
#include <ctime>
#include <cstdlib>
#include <iostream>
int main()
{
srand(time(0));
int randomNumber = ((int[]) {2, 5, 22, 55, 332})[rand() % 5];
std::cout << randomNumber << std::endl;
return 0;
}
Mar 12, 2014 at 9:16am UTC
giblit
(3750)
You kind of double posted.
http://www.cplusplus.com/forum/beginner/125933/
Anyways I think you need to check out how arrays work.
http://www.cplusplus.com/doc/tutorial/arrays/
Topic archived. No new replies allowed.