C++ random numbers!

  • C/++/#
  • Thread starter chmate
  • Start date
  • #1
37
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.
 

Answers and Replies

  • #2
Borek
Mentor
28,815
3,316
Why don't you use rand() to draw index?
 
  • #3
37
0
Hi borek, can you show me an example?

Thank you.
 
  • #4
37
0
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.
 

Related Threads on C++ random numbers!

Replies
9
Views
1K
  • Last Post
Replies
13
Views
3K
Replies
1
Views
3K
  • Last Post
Replies
5
Views
3K
  • Last Post
Replies
3
Views
2K
  • Last Post
2
Replies
31
Views
4K
  • Last Post
Replies
6
Views
6K
Replies
3
Views
8K
Replies
12
Views
2K
  • Last Post
Replies
7
Views
4K
Top