C++——随机数 随机丢包

本文介绍了C++中生成随机数的方法,包括使用rand()和srand()函数,以及如何生成指定范围内的随机数。同时,讨论了在HTM11.0解码端模拟随机丢包的实现,用于中间视点纹理图的丢包测试,代码来源于JCTVC-H0072。
摘要由CSDN通过智能技术生成

1.随机数
rand()函数, srand()函数

rand():会返回范围在0至RAND_MAX 间的随机数;
rand()%x:会返回范围在0至x-1间的随机数;

srand(seed)设好随机数种子(seed);
srand((unsigned) time(NULL));
srand(unsigned(time(0)));

x = rand()%11; //产生1~10之间的随机整数
y = rand()%51 - 25; //产生-25 ~ 25之间的随机整数
z = ((double)rand()/RAND_MAX)*(b-a) + a; //产生区间[a,b]上的随机数

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 
using namespace std;

void main()
{
    ofstream out("E:\\1.txt");
    for (int i=0;i<100;i++)
    {
        out<<1;
    }

    srand(unsigned(time(NULL)));
    int j1;
    for (int i=0;i<5;i++)
    {
        j1=rand()%101;
        cout<<j1<<" ";
    }
}

2.随机丢包
因为在做整帧丢失的恢复,在HTM11.0解码端的输入是stream.bit,需要丢包软件进行随机丢包,代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <math.h>
#include <float.h>
#include <ctype.h>

#define OPTIONCHAR '-'
#define MAXNALUNITSIZE 1000000

static FILE *errfile, *infile, *outfile;
static unsigned char nalu [MAXNALUNITSIZE];
static int nalutypecountread[64], nalutypecountwrite[64];

static int nalusread=0, nalusreadValid=0,naluswritten=0,nalusLost=0;
bool var=0;

void parsecmd (int ac, char *av[]);     
int readnalu(unsigned char *buf);       
void writenalu (unsigned char *buf, int size);      
int nal_unit_type(unsigned char *buf);      
int naluloss();                 
int startcode();                                                                                    
void cleanup();             

int main(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值