我想生成1到4,4范围内的随机数,包括.
这是我的代码:
int num = r.nextInt(4) + 1; //r is instance of Random.
Run Code Online (Sandbox Code Playgroud)
但是,我在循环中运行上面的代码,不想重复随机数.现在发生的事情通常是我得到的:
1,1,1,2,3,1,4,2,2,1,4,2,4,4,2,1,4,3,3,1,4,2,4,1作为我的输出.
这里,虽然数字在范围(1-4)内是随机的,但是经常像前3次迭代中的数字"1"那样重复.
我正在寻找的是一种在循环中获得非重复随机数的方法.我知道的一个简单方法是在当前迭代之前保留最后一个随机数并进行比较,但我相信必须有更好的解决方案.
提前致谢.
读到现在,我已经了解到将UI元素添加到UITableViewCell的contentView是一个很好的做法.但通常,我看到这是使用代码完成的.我想知道如果使用Interface Builder添加仍然添加到contentView的元素?
Let's say I have a model User.
I want to create a BehaviorSubject of type User as below:
private userSource = new BehaviorSubject<User>({});
Run Code Online (Sandbox Code Playgroud)
With this statement I'm getting the following error:
Argument of type '{}' is not assignable to parameter of type 'User'
Run Code Online (Sandbox Code Playgroud)
Could anyone help me how define a BehaviorSubject of type User