Implement Rand10() Using Rand7() 用 Rand7() 实现 Rand10()

已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数。

不要使用系统的 Math.random() 方法。

示例 1:

输入: 1
输出: [7]

示例 2:

输入: 2
输出: [8,4]

示例 3:

输入: 3
输出: [8,1,10]

提示:

  1. rand7 已定义。
  2. 传入参数: n 表示 rand10 的调用次数。

进阶:

  1. rand7()调用次数的 期望值 是多少 ?
  2. 你能否尽量少调用 rand7() ?

思路:这道题自己确实是想不出来,只能借用网上的解法,所以这里借用博主的博客:https://blog.youkuaiyun.com/dyoyo90/article/details/12784861

如果由一个大的随机数产生一个小的随机数,比如rand10()要产生rand5(),那么可以用取余的操作,如果选出的随机数在1~5之间,那么保留,否则就继续重新选。

rand7->rand49

现在的问题是如何由一个小的随机数产生一个大的随机数,比如这道题的rand7()产生rand10(),那么我们先做这样的操作:res=7*(rand7()-1)+rand7(),这样res可以产生1~49的49个随机数,为什么呢?分解来看:7*(rand7()-1)可以产生0,7,14,21,28,35,42这7个数,那么在加上随机产生的1~7这7个随机数,就可以产生1~49这49个随机数。类似于先把数据的间隔拉大而能刚好进行本身的线性插值,这样就可以产生一个较大的随机数。

rand49->rand10

其实到这里已经很简单了,能产生1~49的数,那么我们只用随机到1~10的数即可,其余的数再重新随机取就行了。但是这样的复杂度有点高,相当于只保留了10(总49)的数据,时间复杂度为O(49/10)=O(4.9)。其实我们可以取1~40的数,然后再对取到的数取余操作即可,这样的复杂度为O(49/40)=O(1.225)。

参考代码:

class Solution {
public:
    int rand10() {
        int res;
        do {
            res = 7 * (rand7() - 1) + rand7();
        } while (res > 40);
        return res % 10 + 1;
    }
};

 

Here is an example implementation of a linear regression model using PyTorch and Autograd for optimization: ```python import torch import numpy as np # Generate some random data np.random.seed(42) x = np.random.rand(100, 1) y = 2 + 3 * x + 0.1 * np.random.randn(100, 1) # Convert data to PyTorch tensors x_tensor = torch.from_numpy(x).float() y_tensor = torch.from_numpy(y).float() # Define the model class LinearRegression(torch.nn.Module): def __init__(self): super(LinearRegression, self).__init__() self.linear = torch.nn.Linear(1, 1) def forward(self, x): return self.linear(x) model = LinearRegression() # Define the loss function criterion = torch.nn.MSELoss() # Define the optimizer optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # Train the model num_epochs = 1000 for epoch in range(num_epochs): # Forward pass y_pred = model(x_tensor) loss = criterion(y_pred, y_tensor) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() # Print progress if (epoch+1) % 100 == 0: print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item())) # Print the learned parameters w, b = model.parameters() print('w =', w.item()) print('b =', b.item()) ``` In this example, we define a linear regression model as a subclass of `torch.nn.Module`, with a single linear layer. We use the mean squared error loss function and stochastic gradient descent optimizer to train the model on the randomly generated data. The model parameters are learned through backpropagation using the `backward()` method, and are optimized using the `step()` method of the optimizer. After training, we print the learned values of the slope and intercept parameters.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值