# 随机数

1）stdlib-rand()，每次都是一样的随机数

```c
#include<stdio.h>
#include<stdlib.h>

int main()
{
       for(int i=0;i<10;i++){
             printf("%d \n",rand());
    }
  return 0;
}

/*
(base) jiang@jiangxinfadeAir c % ./random_1
16807 
282475249 
1622650073 
984943658 
1144108930 
470211272 
101027544 
1457850878 
1458777923 
2007237709 
**/
```

2）一定范围内的随机数

加上`rand()%n`

3）不固定带种子的随机数

通常可以利用time(0)的返回值来当做seed

```c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand()%x)

int main()
{

     srand((int)time(0));
     for(int x=0;x<10;x++)
           printf("%d \n",random(100));
  return 0;
}


/*
(base) jiang@jiangxinfadeAir c % ./random_2
86 
9 
65 
96 
13 
98 
9 
22 
63 
22 
*/
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/c/sui-ji-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
