PDA

View Full Version : C++ random numbers!


chmate
Jun21-10, 04:25 PM
Hello!

I've been trying to create an algorithm which picks a number randomly from array. P.S i have an array like A={1,4,-1,3,-7,2,-14} and I want to pick a number randomly from array.

Any idea?

Thank you.

Borek
Jun21-10, 04:45 PM
Why don't you use rand() to draw index?

chmate
Jun22-10, 01:23 AM
Hi borek, can you show me an example?

Thank you.

chmate
Jun22-10, 01:49 AM
Hello!

I've solved my problem. I've been looking for this part of code:

srand(time(NULL));
random = arr[rand() % (sizeof(arr) / sizeof(arr[0]))];

Thank you.