Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ random numbers!

  1. Jun 21, 2010 #1
    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.
     
  2. jcsd
  3. Jun 21, 2010 #2

    Borek

    User Avatar

    Staff: Mentor

    Why don't you use rand() to draw index?
     
  4. Jun 22, 2010 #3
    Hi borek, can you show me an example?

    Thank you.
     
  5. Jun 22, 2010 #4
    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.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook