相关疑难解决方法(0)

C++中从-9到9的随机数

只是想知道,如果我有以下代码:

int randomNum = rand() % 18 + (-9);
Run Code Online (Sandbox Code Playgroud)

这会创建一个从-9到9的随机数吗?

c++ random numbers

14
推荐指数
3
解决办法
4万
查看次数

生成-1和1之间的随机双精度

我已经做了一段时间并且遇到了很多麻烦.我想生成一个从-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

10
推荐指数
2
解决办法
2万
查看次数

C++ TR1:如何使用normal_distribution?

我正在尝试使用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++ normal-distribution tr1

9
推荐指数
2
解决办法
9925
查看次数

C++:生成高斯分布

我想知道在C++标准库中是否有任何高斯分布数生成器,或者是否有任何代码片段可以通过.

提前致谢.

c++ normal-distribution gaussian

8
推荐指数
3
解决办法
1万
查看次数

基于文本的冒险游戏

我正在监督一个技术营地,其中一个营员为一个基于文本的视频游戏创建了一些代码,他无法显示结果.当程序编译并正确运行时,当选择"治疗"时它不会增加玩家的健康状况,当用户选择"攻击"时我们也会得到零.我对编程知识有限,并且我尽力帮助他,以便他在这里的经历将是愉快和充实的.如果您能提供任何帮助或建议,我们将非常感激.这是代码:

// 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)

c++ class adventure game-engine

7
推荐指数
2
解决办法
5401
查看次数

最小和最大之间的随机双倍

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之间的值.

知道我做错了什么以及如何解决它?

c++ random

5
推荐指数
1
解决办法
2万
查看次数

随机未在范围内声明

我收到错误:

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)

c++ include

5
推荐指数
2
解决办法
3万
查看次数

密码安全浮动

如何在Javascript中生成加密安全浮动?

这应该是一个插件Math.random,范围(0,1),但加密安全.用法示例

cryptoFloat.random();
0.8083966837153522
Run Code Online (Sandbox Code Playgroud)

在javascript中保护随机数?展示了如何创建加密安全的Uint32Array.也许这可能会以某种方式转换为浮动?

  • 关于如何从int转换,Mozilla Uint32Array文档并不完全清楚.
  • 谷歌也没有说到这一点.
  • Float32Array.from(someUintBuf); 总是给出一个整数.

javascript random cryptography

5
推荐指数
1
解决办法
381
查看次数

从/ dev/urandom获取一个浮点数

如何从中获取随机浮点值/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

c c++ linux floating-point

4
推荐指数
1
解决办法
2387
查看次数

如何从字符串中获取0到1700之间的随机数?

目前我正在与之合作

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++

3
推荐指数
1
解决办法
119
查看次数