How to organize a random number algorithm for an array of 10 * 10 so that the elements of this array have both negative and positive values? (on si)
1 answer
If you are satisfied with the range from -RAND_MAX / 2 ... 0 ... RAND_MAX / 2, then just subtract from the result random () RAND_MAX / 2. And then scaling the result to -N ... 0 ... N is not a problem.
- and how to organize it in the program? that's where the recording goes ... srand (time (NULL)); for (i = 0; i <10; i ++) for (j = 0; j <10; j ++) arr [i] [j] = rand ()% 100; / * I understand this so that the range of choice of values is from 0 to 100, and how to write here from -50 to 50 for example? * / - vinnypaf
- Well, subtract 50 =) Get from -50 to 50: (0 - 50) to (100 - 50). - Alexey Sonkin
- uh ... then I ask the question like this =) (sorry, I'm just a beginner) here is rand ()% 100; , what do the brackets mean and why are they empty, and what does the% sign mean? - vinnypaf
- oneThe function arguments are written in brackets. Well, like sin (0) is the sine of zero. abs (10) - module 10. No arguments are needed for the rand function, here are the brackets and empty. The percent sign is the remainder of the division. By definition, rand will return any number in size (from 0 to the maximum possible for the type int) and in order to bring it to a certain range, you need to get the remainder of the division by this range. about this, please read the tutorial. - Alexey Sonkin
- thanks a lot! ^^ - vinnypaf
|