C++ 跟踪C中的整数列表,并从列表中采样
让我们简单地说,我有一个生成随机整数的函数。生成的每个数字我都想钉在列表的末尾 然后,在最后,我想从这个列表中随机抽取一个数字 我是C语言的新手,对单链表和指针参数很在行,所以任何建议都是欢迎的C++ 跟踪C中的整数列表,并从列表中采样,c++,c,C++,C,让我们简单地说,我有一个生成随机整数的函数。生成的每个数字我都想钉在列表的末尾 然后,在最后,我想从这个列表中随机抽取一个数字 我是C语言的新手,对单链表和指针参数很在行,所以任何建议都是欢迎的 > >编辑:我没有问题切换到C++,如果有结构可以帮助我的话。我只需要清单和抽样 因此,如果您知道要将多少元素放入列表中 那你就可以这么做了 double array[100] // or whateversize you want for(int i = 0; i < 100; i++){
<> > >编辑:我没有问题切换到C++,如果有结构可以帮助我的话。我只需要清单和抽样 因此,如果您知道要将多少元素放入列表中 那你就可以这么做了
double array[100] // or whateversize you want
for(int i = 0; i < 100; i++){
array[i] = yourRandomNumber;
}
double index = Rand(0,100);
return array[index];
在c语言中使用std::vectors++
#include <iostream>
#include <vector>
int main(){
std::vector<double> myVec;
while(cin){ // this lines syntax is not correct because I don't know how you are
// getting your values
myVec.push_back(value);
}
std::cout << myVec[Rand(0, myVec.size())] << std::endl;
}
#包括
#包括
int main(){
std::载体myVec;
while(cin){//这行语法不正确,因为我不知道你怎么样
//获取你的价值观
myVec.push_back(值);
}
std::可以试试看:
#包括
#包括
#包括
#包括
int main(){
srand(时间(空));
int size=0;
int容量=4;
int*data=malloc(容量*sizeof(int));
对于(int i=0;i<1000;++i){
//分配更多空间
如果(大小=容量){
容量*=2;
数据=realloc(数据,容量*sizeof(int));
如果(数据==NULL)
出口(-1);
}
//附加随机数
数据[大小]=兰德();
大小++;
}
//选择一个随机数(错误)
printf(“%d\n”,数据[rand()%size]);
}
现在,为什么这个代码很糟糕?
rand()
非常糟糕。如果您的实现支持,则允许它返回范围[032768)
或更大的随机数
rand()
允许使用相对糟糕的算法生成随机数
rand()%size
不一定是统一的
time(NULL)
是一个糟糕的种子。time(NULL)
以秒的精度返回当前时间。因此,如果我快速运行此代码两次,它通常会返回相同的结果
如果您事先知道您可以接受的点数至少有一个上限,那么您可以使用@Jay答案的变体。否则,您需要某种动态分配。您可以使用链表并在运行时分配节点,但简单地malloc()
数组和realloc()可能更快更容易
如果您发现需要更多空间:
#include <stdlib.h>
double select_one() {
int capacity = 100;
int count;
double rval;
double *accepted = malloc(capacity * sizeof(double));
// accepted == NULL on malloc() failure
count = 0;
while (/* more points to test */) {
double point = /* next point to test */;
if (/* point is accepted */) {
if (count >= capacity) {
/* expand the array */
double *temp;
capacity = 3 * capacity / 2;
temp = realloc(accepted, capacity * sizeof(double));
if (temp == NULL) {
/* need to handle allocation failure to avoid a memory leak */
} else {
accepted = temp;
}
}
accepted[count++] = point;
}
}
/* Note: not perfectly uniform because of the modulus */
rval = accepted[rand() % count];
/* clean up the allocated memory, AFTER selecting the value */
free(accepted);
return rval;
}
#包括
双选一(){
内部容量=100;
整数计数;
双rval;
double*已接受=malloc(容量*sizeof(双));
//malloc()失败时已接受==NULL
计数=0;
同时(/*更多测试点*/){
双点=/*下一个测试点*/;
如果(/*点被接受*/){
如果(计数>=容量){
/*展开阵列*/
双*温度;
容量=3*容量/2;
temp=realloc(可接受,容量*sizeof(双倍));
if(temp==NULL){
/*需要处理分配失败以避免内存泄漏*/
}否则{
接受=温度;
}
}
接受[计数++]=点;
}
}
/*注:由于模量的原因,不完全均匀*/
rval=已接受[rand()%count];
/*选择值后,清除分配的内存*/
免费(接受);
返回rval;
}
用谷歌搜索像“set”和“C”这样的东西真的很难因为这些词有多普遍:/1.你尝试过什么?2.如果数字已经是随机的,为什么你需要从列表中随机抽取?按顺序遍历就可以了。我尝试过一个单链接列表+单独跟踪其大小,然后得到一个随机整数,直到列表的大小,然后遍历整个列表列出拉取所选项目的次数。随机条件只是一个抽象,我附加的数字并不是真正随机的。等等,你已经尝试过链表了吗?这不起作用吗?我只是对C非常陌生。我尝试在C中复制Python行为,但如果有更好的方法,我会非常开放地改变它们方法。我事先不知道数组大小。我基本上是对点进行采样,如果它们满足某些条件,就添加它们。在python中,它只是随机的。choice(list)和list.append(item)你可以用C语言来学习新的东西。C++可以使你的生活变得简单易懂。基本上只是从Python移动到C来加速采样功能。使用C++。尤其是对于这样的问题。你可以使用<代码> C++向量>代码>你可以在代码列表中按< >代码>推/ <代码>到列表的末尾。如果你不忙的话,你能告诉我如何使用向量来完成整个脚本吗?我正在用谷歌搜索它们的用法,但是看到一个实现会很有帮助。
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int size = 0;
int capacity = 4;
int *data = malloc(capacity * sizeof(int));
for (int i=0; i < 1000; ++i) {
// Allocate more space
if (size == capacity) {
capacity *= 2;
data = realloc(data, capacity * sizeof(int));
if (data == NULL)
exit(-1);
}
// Append a random number
data[size] = rand();
size++;
}
// Choose a random number (poorly)
printf("%d\n", data[rand() % size]);
}
#include <stdlib.h>
double select_one() {
int capacity = 100;
int count;
double rval;
double *accepted = malloc(capacity * sizeof(double));
// accepted == NULL on malloc() failure
count = 0;
while (/* more points to test */) {
double point = /* next point to test */;
if (/* point is accepted */) {
if (count >= capacity) {
/* expand the array */
double *temp;
capacity = 3 * capacity / 2;
temp = realloc(accepted, capacity * sizeof(double));
if (temp == NULL) {
/* need to handle allocation failure to avoid a memory leak */
} else {
accepted = temp;
}
}
accepted[count++] = point;
}
}
/* Note: not perfectly uniform because of the modulus */
rval = accepted[rand() % count];
/* clean up the allocated memory, AFTER selecting the value */
free(accepted);
return rval;
}