给出了以下C++代码:
std::vector<int> myVec;
int* num = new int(1);
myVec.emplace_back(std::move(*num));
如果将具有动态存储时间的变量移动到容器(例如向量)中,如果num被销毁(超出范围),是否仍然需要手动删除myVec?如果我用push_back而不是emplace_back,有什么区别吗?
我正在为我的C++类制作一个内存游戏,该游戏将一些字符值随机分配到2D数组的不同部分,以便玩家进行匹配。我有点挣扎,想让这个功能显示游戏板。它应该显示“蒙面”字符/字符在bool中设置为false的下划线,然后当玩家选择要显示的列和行时,将其设置为true以查看实际字符。我现在只是在测试它,它不起作用了。任何调整都是非常感激的。
这是相关代码。
const int ROWS = 4;
const int COLS = 4;
int generate_rand_num(int min, int max);
void display_grid(char grid_array[ROWS][COLS]
我正在试着找到一种从粘贴纸上读取任意行的方法。 string line = "";
WebClient WC = new WebClient();
List<string> TEST = new List<string>();
Random Rand = new Random();
line = WC.DownloadString("Pastebin");
TEST.Add(line);
我正在阅读一个,除了其他内容外,它还谈到了C++中的内存管理。举了一个例子来说明什么是不能做的:
Foo& FooFactory::createBadFoo(int a, int b)
{
Foo aLocalFooInstance(a, b); // creates a local instance of the class Foo
return aLocalFooInstance; // returns a reference to this instance
}
这将无法工作,因为aLocalFooInstance离开作用域并被销毁。好吧,对我来说是有道
当我在Processing.py中运行下面的代码时,我会得到一个超出范围错误的索引,并且我不知道原因。所有的帮助都很感激。
x = 0
y = 0
rand = random(255)
def setup():
size(200, 200)
def draw():
global x, y, rand
loadPixels()
for i in range(0, width):
x += 1
for j in range(0, height):
y += 1
index = (x
我试图将一个字符串数组以对角线方式放入2d字符数组中。我有一个问题,产生我的随机数的范围,以便它将在我的2D数组的索引中。我的代码可以工作,但有时会中断,给出一个"java.lang.ArrayIndexOutOfBoundsException: 60“。
useWords是用户输入的字符串数组,puzzleBoard是由60行30列组成的2d数组。
for (int i = 0; i < userWords.length; i++) {
int r = rand.nextInt(60);
int c = rand.nextInt(puzzleB
我正在生成随机的RGB值
int r = rand.nextInt(255); // [0,255]
int g = rand.nextInt(255); // [0,255]
int b = rand.nextInt(255); // [0,255]
如何将这些整数转换为另一个值(可能是int或十六进制)?这样我就可以像这样设置视图的背景色
int color = getColor(r,g,b);
myView.setBackgroundColor(color);
getColor()方法看起来可能是什么样子?
当我编写生成随机字符串列表的代码时,长度为5个字符,以7为单位:
#include<iostream>
#include<string.h>
using namespace std;
void generate(string*, int);
int main()
{
string* list = new string[7];
generate(list, 7);
return 0;
}
void generate(string* list, int a)
{
for (int i = 0; i < a; i++)
{
Random rand = new Random(10);
Console.WriteLine(rand.Next(100));
这几行代码总是给出95作为输出。当我在第一行"8“中设置构造函数的参数值时,代码总是给出90作为输出。有人能指着我为什么会这样吗?
P.S:我猜这与参数值对Random类对象的作用有关,但是对这种行为有一个具体的洞察力将是非常感谢的。
我的代码运行良好,在C++中使用了很多机制,但是,我想知道是否有任何方法使我的代码更好,一些可能的问题正在使用中。这是一个简单的程序,我昨天开始了。
我还能实现哪些其他机制来使代码更好呢?我在哪里能找到更多关于这些机械的信息?
// IQ rand generator by ashar rahman
#include "stdafx.h" //includes rand and other related commands
#include <stdlib.h> //something windows wants
#include
我在一个网站上看到了以下问题:
how to generate 0 and 1 with 25% and 75% probability, here is corresponding code in c++
int rand50()
{
// rand() function will generate odd or even
// number with equal probability. If rand()
// generates odd number, the function will
// return 1 else it will re
例如,rand()是生成随机数的API。现在,我想在代码中创建自己的rand()。因此,新的rand()应该重载库中已经定义的API。
void getRandomValues () {
int a = rand();
......
......
int b = rand();
}
上面是一个示例代码。
现在我想把上面的代码改为,
void getRandomValues () {
auto a = rand();
......
......
auto b = rand();
}
我想编写自己的rand()函数,它不仅返回整数,还返回其他自定义值
我想知道是什么导致了我的超出范围错误。这并不是每次都会发生,但如果我在输入框中输入100,它最终会发生,然后有时在启动时脚本会给我一个超出范围的错误。
function guns(){
var gcol = SpreadsheetApp.getActiveSheet().getRange('G2:G').clearContent();
var weapons = SpreadsheetApp.getActiveSheet().getRange('F1');
var amountOfGuns = SpreadsheetApp.getActiveSheet().ge
我想创建一个方法,它将生成从0到999的随机且唯一的数字。我试图做的是生成随机的唯一数,然后将它们添加到一个向量中,这样每次我调用该方法时,它都会生成一个随机数,然后将其与之前生成的数进行比较,看看它是否唯一。如果不是,则重复该过程。这就是我的想法,但每次我运行程序时,我都会得到“数组索引超出范围: 0”。
我在类的开头将向量初始化为private Vector<Integer> uniqueNums;
public int returnRandom() {
Random r = new Random();
int rand = r.nextInt(
您好,我已经编写了一个c++程序,它对随机数使用了rand()函数,并给出了奇怪的结果。实际代码中还有更多内容,但代码的基本思想类似于: int main()
{
srand(time(NULL));
priority_queue<long long int> pQueueInts;
const int TRIALS = 1000000;
for (int i = 0; i < TRIALS; i++)
{
long long int holder = randomNumber();
pQueueInt
为了测试和学习的目的,我想修改php rand和mt_rand函数,这些函数位于:ext/Standard.c中。
每次调用rand函数时,我都想给出一个固定的输出,为此,我修改了代码
PHPAPI long php_rand(TSRMLS_D)
{
long ret;
if (!BG(rand_is_seeded)) {
php_srand(GENERATE_SEED() TSRMLS_CC);
}
#ifdef ZTS
ret = php_rand_r(&BG(rand_seed));
#else
# if defined(HAV
我试图通过使用随机数生成器为R、G和B值随机生成数字来创建一个随机颜色,并使用这些值来生成颜色。下面的代码在我的onCreate()方法中:
Random rand = new Random();
// Java 'Color' class takes 3 floats, from 0 to 1.
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b)
我想知道如何在一个固定位置得到一个随机数,所以当我回到那个位置时,我会看到相同的随机数。
首先,我尝试了这样的方法:
int getRand( int x , int y )
{
srand( x * y );
return rand();
}
但是这里-1 * -1和1 *1是一样的,所以我会得到很多对称性。
然后我试着:
int getRand( int x , int y )
{
srand( x * ( 1 << 12 ) + y );
return rand();
}
和
int getRand( int x , int y ) //
在C++中产生随机数的基础是什么?
这背后是否有逻辑或原则呢?
产生的数字是完全随机的吗?
假设我正在运行这个程序:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main()
{
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds