我只知道如何生成随机布尔值(true/false).默认概率为50:50
但是,如何以自己的概率生成真正的假值?假设它以40:60或20:80等概率返回true ...
成员
我想要做的是向右或向左移动一个数字Int32
(不是位!!).
所以如果改变常数:
123456789
Run Code Online (Sandbox Code Playgroud)
通过 3
我应该得到
789123456
Run Code Online (Sandbox Code Playgroud)
所以没有数字丢失,因为我们谈论循环转变.经过一些测试后,我想出了这个方法,它有效:
static uint[] Pow10 = new uint[] { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, uint.MaxValue };
static uint RotateShift10(uint value, int shift)
{
int r = (int)Math.Floor(Math.Log10(value) + 1);
while (r < shift)
shift = shift - r;
if (shift < 0) shift = 9 + shift;
uint x = value / Pow10[shift];
uint i = 0;
while (true)
{
if (x < Pow10[i])
return x + …
Run Code Online (Sandbox Code Playgroud) 我试图计算Pi的第n个数字而不使用Math.Pi
哪个可以指定为参数.我修改了一个现有的算法,因为我喜欢在不使用字符串转换或默认类的情况下找到第N个数字.这就是我的算法目前的样子:
static int CalculatePi(int pos)
{
List<int> result = new List<int>();
int digits = 102;
int[] x = new int[digits * 3 + 2];
int[] r = new int[digits * 3 + 2];
for (int j = 0; j < x.Length; j++)
x[j] = 20;
for (int i = 0; i < digits; i++)
{
int carry = 0;
for (int j = 0; j < x.Length; j++)
{
int num = (int)(x.Length - j - 1);
int …
Run Code Online (Sandbox Code Playgroud) 我期待着改进我的算法,找到给定数字右边的下一个黄金编号.到目前为止我所拥有的是:
int NextPrime(int a)
{
int i, j, count, num;
for (i = a + 1; 1; i++)
{
for (j = 2, count = 0; j <= i; j++)
{
if (i%j == 0)
{
count++;
}
}
if (count == 1)
{
return i;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
经常运行时,这种算法并不那么有效.有人可以提出有关如何加快或改进算法的建议.
我正在寻找一种方法来动态设置整数数组的大小取决于传递的参数.例如,这是伪代码:
int MyFunction(int number)
{
int myarr[amount of digits in number];
}
Run Code Online (Sandbox Code Playgroud)
所以当输入是13456时,那么int array[]
大小应该是5.当我不知道大小的常量时,用C++做最快的方法是什么?
我想询问是否有一种有效的方法来反转整数中的所有set和unset位.例如:如果我有整数:
1338842
这与二进制文件相同:
101000110110111011010
Run Code Online (Sandbox Code Playgroud)
如何对此进行反转,使每1位变为0位,每0位变为1位.反过来的结果应该是:
010111001001000100101
Run Code Online (Sandbox Code Playgroud)
这基本上是整数:
758309
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法展示我的尝试,因为我没有.我不知道该怎么做.这就是为什么我希望董事会成员可以给我一些建议.
我有个问题.我在Google上找不到与此相关的任何内容,这就是为什么我希望SOF的专家可以帮助我.
基本上我正在尝试进行睡眠,但这些都是条件.
Task.Delay()
在这种情况下不能使用.我基本上有一个Form fade in
和fade out
之间我喜欢在执行之前等待几秒钟fadeout
.在表格上也是animations
等等所以我需要确保他们在睡觉时不会挂起.
有人知道如何实现这一目标吗?