0

I have tried to fill an array with randoms numbers between zero and nine. The code I have used so far is this.

int a[40], b[40];
for (int i=0; i<39; i++)
{
a=(0 rand() % 9);
b=(0 rand() % 9);
}


Why will this not work?

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by craig_ka
1

First off you are %ing by the wrong number rand()%y will generate a random number between 0 and y-1. Second your loop is only filling in 39 of the 40 values you have allocated to your arrays. Third (0 rand() % 9) will give you an error because there is no operator between 0 and rand(). Fourth although its not necessary, you should seed the random number generator with srand(uint) where the seed given is normally time(NULL) to pass the current time so that each run of the program generates new numbers based off of the current time. This last part would require you to include time.h also.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.