---
# System prepended metadata

title: Some of the Important Fuction
tags: [C++]

---

---
###### tags: `C++`
---
# Some of the Important Fuction
## rand()
1. Need to `#include <cstdlib>`
![](https://i.imgur.com/OOytgO9.png)
> For example `rand()%3+1` the result can be`1` `2` `3`
> However, we can find that if I open the .exe again, the result will be same
### The soluction 
#### 1. Use ==seed==
> We need to use another fuction`srand()`
```C++=
int seed;
cin>>seed;
srand(seed);
cout<< rand()%6+1
``` 
#### 2. Use ==time==
> we can use `time`
> Then we should `#include <ctime>`
> 
```C++
srand(static_cast<int>(time(0)))
```

## setpresion()
> Sometime we need to indiciate the floating-point value shouble be output n digits of precision.

> To do it we need to use `setpresion()`, it's lib is `#include <iomanip>`

![](https://i.imgur.com/k9w2iWV.png)
### Eg
```C++=
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
    float x=1.2255582555655566;
    cout<<setprecision(3)<<fixed;
    cout<<x;
    return 0;
}
```
> **Output:** 1.226
## setw()
> Sometime we need to typeset, we can use `setw()` to do it.


| left justified      | right justified |
| ------------------- | --------------- |
| `cout<<left<<setw(n)` | `cout<<setw(n)`   |
 
 >$n$ represent that the space it conservation.