只是想知道,如果我有以下代码:
int randomNum = rand() % 18 + (-9);
Run Code Online (Sandbox Code Playgroud)
这会创建一个从-9到9的随机数吗?
我已经做了一段时间并且遇到了很多麻烦.我想生成一个从-1到1的随机值进行计算.我不能使用%运算符,因为它仅适用于整数.我也试过用,fmod()
但我也遇到了困难.
我试图使用的是......
double random_value;
random_value = fmod((double) rand(),2) + (-1);
Run Code Online (Sandbox Code Playgroud)
看起来它似乎不正确.我也试着用时间播种srand,但我认为我在那里做错了,因为它一直在抛出这个错误:
"error: expected declaration specifiers or '...' before time"
Run Code Online (Sandbox Code Playgroud)
码:
srand((unsigned) time(&t));
Run Code Online (Sandbox Code Playgroud)
任何有关这些问题的帮助都会受到赞赏.
我正在尝试使用C++ STD TechnicalReport1扩展来生成正常分布后的数字,但是这段代码(改编自本文):
mt19937 eng;
eng.seed(SEED);
normal_distribution<double> dist;
// XXX if I use the one below it exits the for loop
// uniform_int<int> dist(1, 52);
for (unsigned int i = 0; i < 1000; ++i) {
cout << "Generating " << i << "-th value" << endl;
cout << dist(eng) << endl;
}
Run Code Online (Sandbox Code Playgroud)
只打印1"Generating ..."日志消息,然后永远不会退出for循环!如果我使用我注释掉的发行版,它会终止,所以我想知道我做错了什么.任何的想法?
非常感谢!
我想知道在C++标准库中是否有任何高斯分布数生成器,或者是否有任何代码片段可以通过.
提前致谢.
我正在监督一个技术营地,其中一个营员为一个基于文本的视频游戏创建了一些代码,他无法显示结果.当程序编译并正确运行时,当选择"治疗"时它不会增加玩家的健康状况,当用户选择"攻击"时我们也会得到零.我对编程知识有限,并且我尽力帮助他,以便他在这里的经历将是愉快和充实的.如果您能提供任何帮助或建议,我们将非常感激.这是代码:
// Test for hard stuff.cpp : Defines the entry point for the console application.
//
// Bigger proj
// Constructors will make characters with rolling statistics
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// declaring function for hit power
//int power( int str, int def);
int command;
class character
{
public:
character();
//~character();
string name;
float str;
float def;
float health; // hit points
float regen; // health regen amount
float …
Run Code Online (Sandbox Code Playgroud) double get_random(double min, double max) {
/* Returns a random double between min and max */
return min * ((double) rand() / (double) RAND_MAX) - max;
}
Run Code Online (Sandbox Code Playgroud)
这是我在最小和最大之间产生随机双打的功能.但是,当我打电话时get_random(-1.0, 1.0);
,我得到-2.0和-1.0之间的值.
知道我做错了什么以及如何解决它?
我收到错误:
tester.cpp | 20 |错误:在此范围中未声明'rand'
我想念这里的东西吗?
void tester::volumeset(bool A_B, int i, int v)
{
if (A_B == true)
{
A_volumn[i] = rand(v+1);
}else{
B_volumn[i] = rand(v+1);
}
}
Run Code Online (Sandbox Code Playgroud) 如何在Javascript中生成加密安全浮动?
这应该是一个插件Math.random
,范围(0,1),但加密安全.用法示例
cryptoFloat.random();
0.8083966837153522
Run Code Online (Sandbox Code Playgroud)
在javascript中保护随机数?展示了如何创建加密安全的Uint32Array.也许这可能会以某种方式转换为浮动?
Float32Array.from(someUintBuf);
总是给出一个整数.如何从中获取随机浮点值/dev/urandom
?
如果我只是使用演员,说:
int fd = ::open("/dev/urandom", O_RDONLY);
uint32_t n;
read(fd, &n, sizeof(n));
float f = n;
Run Code Online (Sandbox Code Playgroud)
......我不确定我是否有可移植性的保证,因为我不知道大数值n
是否必然可以表示为f
?MAXUINT保证可以表示为float
?
目前我正在与之合作
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
double RangeFinder::getRange(void) {
return fRand(0, 1700);
};
char range[12];
double r = rangeFinder.getRange();
snprintf(range, 11, "range: %d", r);
Run Code Online (Sandbox Code Playgroud)
但它不给我之间0
,1700
它给我之间-99
,999
(我认为).我有一种感觉,msb正在将数千个地方解释为负面.
有什么问题?
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
double RangeFinder::getRange(void) {
return fRand(0, 1700);
}; …
Run Code Online (Sandbox Code Playgroud) c++ ×8
random ×3
c ×2
adventure ×1
class ×1
cryptography ×1
game-engine ×1
gaussian ×1
include ×1
javascript ×1
linux ×1
numbers ×1
tr1 ×1