Generating Random Numbers in C++

  • Context: C/C++ 
  • Thread starter Thread starter chmate
  • Start date Start date
  • Tags Tags
    C++ Numbers Random
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 3K views
chmate
Messages
37
Reaction score
0
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.
 
Physics news on Phys.org
Hi borek, can you show me an example?

Thank you.
 
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.