小编col*_*sta的帖子

创建具有偶概率的随机int生成器

在下面的代码,我希望确保有偶数theImageRand等于theImage或theImage2的机会,但我意识到,在1和100之间有更多数字等于2模1比2模0,所以被选择theImage不成比例的时间.

这是我遇到的最简单的想法,但也许有一个功能可以做到这一点更容易?我也在想我能找到一个符合我要求的数字并把它放到randi(n)中.

xRand = randi(100);
    if mod(xRand,2) == 1 
       theImageRand = theImage;
    elseif mod(xRand,2) == 0
       theImageRand = theImage2;
    end
Run Code Online (Sandbox Code Playgroud)

如果我能更清楚地解释,请告诉我.提前致谢.

random matlab mod

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

嵌套while循环的时间复杂度

我对如何在此语句中确定 while 循环的时间复杂度感到困惑:

procedure P (integer n);
 for (i: 1 to n)
   x := n;
   while (x > 0)
         x := x - i;
Run Code Online (Sandbox Code Playgroud)

我知道 for 循环运行 (n-1) 次。起初我认为 while 循环会运行 n 次,因为我将 i 误认为是 1,但事实并非如此。我一直在输入数字以查看程序何时停止,但没有看到一致的模式。我注意到随着 n 的增加,while 循环运行的时间更长(但不是很多)所以这可能是对数的吗?提前致谢。

algorithm big-o loops time-complexity

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

标签 统计

algorithm ×1

big-o ×1

loops ×1

matlab ×1

mod ×1

random ×1

time-complexity ×1