C/C++ 将数据乱序
来源:互联网 发布:低碳钢的弹性模量算法 编辑:程序博客网 时间:2024/06/05 04:36
我们在实现某些应用或者做测试的时候,需要将某些数据乱序。例如1,2,3三个数,随机乱序后,可能是2,1,3。
最简单的方法是:输入一系列数,顺序访问每个位置的数,将当前位置的数与一个随机数相关的位置进行内容交换。
纯C实现
#include <stdio.h>#include <stdlib.h>#include <inttypes.h>// n : the length of an arrayint rand_id(int n){ return rand() % n;}// data : 1-D // size_ele : size of per element// len : the length of datavoid rand_perm(uint8_t* data, uint8_t size_ele, int len, int (*rand_id)(int)){ int i, j, idx; uint8_t *cptr = data; uint8_t *tmp = (uint8_t*)malloc(size_ele); for (i = 0; i < len; i++){ idx = rand_id(len); for (j = 0; j < size_ele; j++){ tmp[j] = data[idx*size_ele + j]; } for (j = 0; j < size_ele; j++){ data[idx*size_ele + j] = cptr[j]; } for (j = 0; j < size_ele; j++){ cptr[j] = tmp[j]; } cptr += size_ele; } free(tmp); tmp = 0;}#define __N 4int main(int argc, char** argv){ // test int int32_t a[__N] = {10000000,20000000,30000000,40000000}; rand_perm((uint8_t*)a, sizeof(a[0]), __N, rand_id); for (int i = 0; i < __N; i++){ printf("%d ", a[i]); } printf("\n"); // test float float b[__N] = {1.E-6f, 2.E-6f, 3.E-6f, 4.E-6f}; rand_perm((uint8_t*)b, sizeof(b[0]), __N, rand_id); for (int i = 0; i < __N; i++){ printf("%f ", b[i]); } printf("\n"); // test double double c[__N] = { 1.E-6, 2.E-6, 3.E-6, 4.E-6 }; rand_perm((uint8_t*)c, sizeof(c[0]), __N, rand_id); for (int i = 0; i < __N; i++){ printf("%f ", c[i]); } printf("\n"); return 0;}
输出结果
10000000 40000000 30000000 200000000.000001 0.000002 0.000004 0.0000030.000002 0.000004 0.000003 0.000001
纯C++
C++标准里有随机洗牌函数,我们可以直接调用。
#include <iostream>#include <algorithm>using namespace std;#define __N 4int main(int argc, char** argv){ // test int int a[__N] = { 10000000, 20000000, 30000000, 40000000 }; std::random_shuffle(a, a + __N); for (int i = 0; i < __N; i++){ cout << a[i] << " "; } cout << endl; // test float float b[__N] = { 1.E-6f, 2.E-6f, 3.E-6f, 4.E-6f }; std::random_shuffle(b, b + __N); for (int i = 0; i < __N; i++){ cout << std::fixed << b[i] << " "; } cout << endl; // test double double c[__N] = { 1.E-6, 2.E-6, 3.E-6, 4.E-6 }; std::random_shuffle(c, c + __N); for (int i = 0; i < __N; i++){ cout << std::fixed << c[i] << " "; } cout << endl; return EXIT_SUCCESS;}
输出结果
10000000 20000000 40000000 300000000.000004 0.000001 0.000003 0.0000020.000003 0.000001 0.000004 0.000002
0 0
- C/C++ 将数据乱序
- C语言随机数,乱序问题
- C语言中结构体(struct)乱序初始化
- C语言中struct数组乱序赋值
- C#--将数据写入DataTable[学习笔记]
- 如何将c结构数据传给lua
- [C] 将数据写入到文件
- 如何将c结构数据传给lua
- C语言:将数据保存到文本文件
- C语言--将数据输出到文本文件
- 一锅乱炖之C/C++
- 数据如何乱序排列
- 【Shell】将txt中内容乱序
- 将txt文本读入C中(txt数据量大),将C中数据写入txt
- Linux C中数组,结构体的指定初始化(乱序初始化)
- C语言结构体声明中冒号的使用(占位符) & C结构体的乱序初始化
- 将sql server中的数据倒入Excel(c#)
- 将sql server中的数据倒入Excel(c#)
- Oracle 12C 在线重命名和迁移数据文件
- WLAN and WiFI
- eclipse 项目打包出现错误export aborted because fatal lint errors were found.
- s3sh3环境配置
- 面向对象C++(从生物学理解继承 )
- C/C++ 将数据乱序
- MyBatis insert操作返回主键
- tomcat7与JSTL冲突
- Codeforces Round #336 (Div. 2)
- 文章标题
- Linux中的无线架构概述
- XML名字空间
- vijos P1485传球游戏(递推)
- 用MATLAB将矩阵数据写入txt文件中,打开乱码原因