Sunday, July 12, 2009

How to generate random numbers and store into array?

I'd like to use the random number generator and store it into an array of 10 in my C++ program. How do I do this? I'd like the numbers it gets to be between 0 and 1000. Thanks

How to generate random numbers and store into array?
First, declare your array, like





int array[999];





And then, use the rand() function to create your random numbers. You'll need to include stdlib.h and time.h for the functions to work.





Use a for loop that goes through your array, adding each of the rand() numbers to the array.





That's it!


No comments:

Post a Comment