#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <math.h> int main() { int i; int diceRoll; for(i=0; i < 20; i++) { printf("%d \n", rand()); } return 0; }
This is the code I wrote in c (codeblocks) to get random numbers, the problem is that I always get the same sequence: 41,18467,6334,26500etc.
41,18467,6334,26500
I'm still learning, so try explaining how you are talking to 8 year old D:
You get the same sequence every time because the seed for the random number generator is not set. You need to call srand(time(NULL))as follows:
srand(time(NULL))
int main() { srand(time(NULL)); ....
. , "", "" . , , . , , . , , . , , , - (NULL). , ( ) . , . , (srand (time (NULL)) , , . , . , .
#, ++. ctime. , rand() . #include random , . !
, , <ctime> srand(time(NULL));.
<ctime>
srand(time(NULL));
-, , rand(), : rand()% x 0 x-1. , rand() % 6 + 1.
rand() % 6 + 1
srand((unsigned)(time(NULL)) , .
srand((unsigned)(time(NULL))
Modulo rand()%10means that you get any number starting with 0, up to the fact that you are modulo -1. So, in this case 0-9, if you want 1-10:rand()%10 + 1
rand()%10
0
-1
0-9
1-10
rand()%10 + 1
int main() { int i; int diceRoll; srand((unsigned)(time(NULL)); for(i=0; i < 20; i++) { printf("%d \n", rand() % 10); //Gets you numbers 0-9 } return 0; }
Source: https://habr.com/ru/post/1649070/More articles:Python pandas - remove group based on NaN collective number - pythonHow can I blur dynamic content behind a div? - javascriptPython pandas - удалить группы на основе порогового значения NaN - pythonr: conditionally replace values in a subset of columns - replaceНабор групп LINQ произвольной решеткой - c#Slow performance in React Native with Redux - performanceE / libEGL: cache file failed CRC check - androidStrange behavior with rand () - cpandas: create pivot table in two different sizes? - pythonHow to save one body on top of another using Pymunk - pythonAll Articles