非重复随机数发生器 [英] Non-repeating random number generator

查看:73
本文介绍了非重复随机数发生器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个不会重复给出的数字的数字生成器 已经(C ++).

I'd like to make a number generator that does not repeat the number it has given out already (C++).

我所知道的是:

int randomgenerator(){
  int random;
  srand(time(0));
  random = rand()%11;
  return(random);
} // Added this on edition

该功能为我提供了多余的数字.

That function gives me redundant numbers.

我正在尝试创建一个问卷调查程序,该程序以随机​​顺序给出10个问题,我不希望任何问题再次出现.

I'm trying to create a questionnaire program that gives out 10 questions in a random order and I don't want any of the questions to reappear.

有人知道语法吗?

推荐答案

我会做什么:

  • 生成长度为N的向量,并用值1,2,... N填充它.
  • 使用 std :: random_shuffle .
  • 如果您说30个元素而只想要10个元素,请使用向量中的前10个元素.

我不知道问题是如何存储的,所以..:)

I have no idea how the questions are being stored, so.. :)

我假设问题是通过随机访问存储在向量中或类似的东西中.现在,我生成了10个不会重复的随机数:7、4、12、17、1、13、9、2、3、10.

I am assuming the questions are being stored in a vector or somesuch with random access. Now I have generated 10 random numbers which don't repeat: 7, 4, 12, 17, 1, 13, 9, 2, 3, 10.

我将这些用作问题向量的索引:

I would use those as indices for the vector of questions:

std::vector<std::string> questions;
//fill with questions
for(int i = 0; i < number_of_questions; i++)
{
    send_question_and_get_answer(questions[i]);
}

这篇关于非重复随机数发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆