1

I'm trying to

  1. Get rid of the info in an array with 10 "spots".
  2. Fill the array with (10) random numbers

My code till time

int main()
{
 int numbers[10] = { 0 };
 int randNumber = 0;
 int i = 0;
 for (int i = 0; i <= 10; i++)
 {
 srand(time(NULL));
 randNumber = rand() % 10 + 1;
 printf("Random number saved in the array: %d\n", randNumber);
 i++;
 }
 getchar();
 getchar();
 return 0;
}
Sourav Ghosh
135k17 gold badges192 silver badges271 bronze badges
asked Sep 27, 2016 at 12:37
5
  • Assign the values to the array. Commented Sep 27, 2016 at 12:42
  • Alright, but if the random-function is outside of the for-loop. Will it not just create one random number? (atleast it does now when I have moved it outside). My intention for putting it inside the for-loop was to create a new random number each time the loop runs. Commented Sep 27, 2016 at 12:55
  • srand() does not create the random numbers. rand() actually gives you the random numbers. Commented Sep 27, 2016 at 13:06
  • srand(time(NULL)); means "pick the pseudo random number sequence #1474983934" (#1474983935 in the next second). Afterwards, rand() means "pick the next number in the selected sequence". If you always restart the same sequence and always pick the first number thereof, you always get the same number. Solution: move the srand() call outside the loop. Commented Sep 27, 2016 at 13:47
  • Do your numbers have to be in the range 1 to 10? If so, you might want to start out by assigning values sequentially (for ( i = 0; i < 10; i++ ) numbers[i] = i+1;), then do a random shuffle on the array. Commented Sep 27, 2016 at 13:58

2 Answers 2

3

First of all, you need to move the srand(time(NULL)); out of the loop.

Otherwise, because, time() has a time granularity of 1 second, in a second, if called multiple times in the loop (within a second, probably), it will re-initialize the PNRG with the same seed and all the next call to rand() will give you the same random number.

Now, once you have the random numbers, you need to assign it to the each array member like numbers[i] = randNumber; inside the loop, but there's more to it. Your loop, at present is off by one. You need to change

 for (int i = 0; i <= 10; i++)

to

 for (int i = 0; i < 10; i++)

to stay within bounds.

answered Sep 27, 2016 at 12:39
Sign up to request clarification or add additional context in comments.

Comments

3

Your array's size is 10, and this loop runs 11 times, causing an overflow. This will solve it:

for (int i = 0; i < 10; i++)

Also remove the increasing of the loop's iterator, i, from inside the loop body. Remove the line:

i++;
answered Sep 27, 2016 at 12:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.