题目:Math.random()可以产生一个在0和1之间的数,请写出产生一个在125-175之间整数的语句。

本文介绍如何使用Java的Math.random()方法或Random类生成在特定区间内的随机整数,详细解析了数学原理及代码实现。

题目:Math.random()可以产生一个在0和1之间的数,请写出产生一个在125-175之间整数的语句。

方法:

int a = (int)(Math.random()*51)+125;

解释:

Math.random()返回的是[0.0,1.0)之间的double类型的数字,我们使用Math.random()乘以51返回的就是[0.0,51.0)之间的double类型的数字,由于int类型的数据是向0取整的,如果我们对这个集合中的数字进行强制转换,那么(int)(Math.random()*51)返回的就是[0,50]之间的int类型的数字,我们在这个集合之上加上125,返回的就是[125,175]之间的int整型数字。

拓展:

如果不要求使用Math.random(),也可以这样做:

Random random  = new Random();
int a = random.nextInt(51)+125;

解释:

random.nextInt(51)会返回0~50之间的整数,然后该整数加上125,结果正好是125~175之间的数字

2.1坡面海域测线布设 利用多波束测深模型进行测线布设。坡面海域测线布设需确定两方面——测线延伸方向相邻测线间距。首先确定测线的延伸方向。由1.3节可知,当测线沿坡面海域等深线方向延伸时,条带的覆盖宽度不变。故在此条件下,相邻测线间的重叠率也不变。可以控制所有的重叠率均为最小临界值10%,从而使待测海域内测线的总长度最短<sup>[[1]]</sup>,所以确定测线沿等深线方向延伸。 在文献[8]中明确规定,使用多波束测深仪时,测线的方向布设应平行于等深线总方向,佐证了前文的判断,所以本文选择平行于等深线的方向设计测线,运用控制变量法来控制条带重叠率。 运用控制变量法,控制条带重叠率为10%。再通过迭代算法,自东向西依次求出每条测线的坐标。本文在测线沿等深线延伸的条件下,通过确定条带重叠率10%其中一条测线距海域中心点的东西向坐标,即可确定另一条测线的位置,具体位置可参考图1。 本文规定向东为正方向,自东向西确定第一条测线的位置。第一条测线的东侧条带边界应该待测海域的东侧边界重合。这样既不会浪费测深条带的覆盖宽度,也不会出现漏测的情况。设待测海域的南北长为\( a \),东西长为\( b \),海域中心点的横坐标为\( 0 \),\( x_n \)为自东向西第\( n \)条测线的横坐标,因为海域东边界第一条测线的距离等于第一条测线东侧条带在海平面上的投影宽度,所以: $$\frac{b}{2} - x_1 = \frac{D\sin(\theta/2) \cos \alpha}{\cos(\theta/2 - \alpha)} \tag{15}$$ 海水深度\( D \)海域中心点深度\( D' \)坡度有关,即: $$D = D' - x_1\tan\alpha \tag{16}$$ 其中,海域中心点处的海水深度\( D' = 110 \ \text{m} \)。由式(15)式(16)即可求出第一条测线海域中心点的距离\( x_1 \)。 为了使测线的长度最短,本文先控制条带间的重叠率为最小临界值\( \eta = 10\% \),即: $$\eta = 1 - \frac{z_n - z_{n+1}}{(AC + EF) \cos \alpha} = 10\% \tag{17}$$ 其中, $$AC + EF = \frac{(D' - x_{n+1} \tan \alpha) \sin(\theta/2)}{\cos(\theta/2 - \alpha)} + \frac{(D' - x_n \tan \alpha) \sin(\theta/2)}{\cos(\theta/2 + \alpha)} \tag{18}$$ 在重叠率\( \eta \)\( x_1 \)确定的情况下,基于迭代算法<sup>[[2]]</sup>,将\( x_1 \)作为初始点,联立式(17)式(18),即可求出\( x_2 \)的值,并将\( x_2 \)作为下次迭代的初始点进行计算,不断用变量的旧值递推新值,以此类推,\( x_3, x_4, \dots, x_n \)都可以被依次求出。当到达终止条件,即\( |x_n| = 3704 \ \text{m} \)时,停止迭代,此时测线海域中心点的距离大于2海里。 依照迭代算法对\( x_n \)进行求解,得到35条测线坐标。计算第34条测线\( x_{34} \)的条带西侧边界,联立式(2)式(16)并代入\( x_{34} = -3479.0 \ \text{m} \),可得第34条测线西侧条带宽\( AB_{34} = 365.0 \ \text{m} \),故\( |x_{34}| + AB_{34} = 3844.0 \ \text{m} > 3704 \ \text{m} \)。第34条测线的东侧条带已经覆盖了待测海域的东侧边界,故不需要第35条测线,将其舍去。 本文所设计的测线如表3所示,可以将待测海域完全覆盖。 表3 问题3的测线坐标/m | \( x_1 \) | \( x_2 \) | \( x_3 \) | \( x_4 \) | \( x_5 \) | \( x_6 \) | \( x_7 \) | \( x_8 \) | \( x_9 \) | \( x_{10} \) | \( x_{11} \) | \( x_{12} \) | | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :---------: | :---------: | :---------: | | 3681.5 | 3637.3 | 3589.3 | 3537.2 | 3480.7 | 3419.4 | 3352.9 | 3280.7 | 3202.4 | 3117.4 | 3025.2 | 2925.1 | | \( x_{13} \) | \( x_{14} \) | \( x_{15} \) | \( x_{16} \) | \( x_{17} \) | \( x_{18} \) | \( x_{19} \) | \( x_{20} \) | \( x_{21} \) | \( x_{22} \) | \( x_{23} \) | \( x_{24} \) | | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | | 2816.5 | 2698.7 | 2570.8 | 2432.0 | 2281.4 | 2118.0 | 1940.7 | 1748.3 | 1539.5 | 1312.9 | 1067.1 | 800.3 | | \( x_{25} \) | \( x_{26} \) | \( x_{27} \) | \( x_{28} \) | \( x_{29} \) | \( x_{30} \) | \( x_{31} \) | \( x_{32} \) | \( x_{33} \) | \( x_{34} \) | | | | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | :---------: | ---- | ---- | | 510.8 | 196.7 | -144.2 | -514.1 | -915.5 | -1351.1 | -1823.7 | -2336.6 | -2893.1 | -3479.0 | | | 以上内容均由AI搜集总结并生成,仅供参考 import math import pandas as pd import numpy as np import random import copy import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False theta=120 alpha=1.5 D_center=110 theta_rad = math.radians(theta) alpha_rad = math.radians(alpha) length_n_mile=2 #南北长(海里) width_n_mile=4 #东西宽(海里) overlap_min=0.1 overlap_max=0.2 length_m=length_n_mile*1852 #南北长(米) width_m=width_n_mile*1852 #东西宽(米) # μ(mu) def get_mu(beta): if 0 <= beta < 90: mu = 90 - beta elif 90 <= beta < 180: mu = beta - 90 elif 180 <= beta < 270: mu = 270 - beta elif 270 <= beta < 360: mu = beta - 270 else: raise ValueError("beta 应在 [0, 360) 范围内") return mu # λ(lambda) def get_lambda(alpha_rad, beta): if beta in [0, 180]: return alpha_rad elif beta in [90, 270]: return 0.0 else: mu = get_mu(beta) mu_rad = math.radians(mu) lambda_rad=math.atan(math.tan(alpha_rad)*math.cos(mu_rad)) return lambda_rad # ∠1∠2 def get_angles(theta_rad, lambda_rad): angle1 = (math.pi - theta_rad) / 2 - lambda_rad angle2 = (math.pi - theta_rad) / 2 + lambda_rad return angle1, angle2 #ω(omega) def get_omega(alpha_rad,mu): mu_rad = math.radians(mu) omega_rad=math.atan(math.tan(mu_rad)*math.cos(alpha_rad)) return omega_rad #深度(坐标系的建立) def get_depth(x,beta): mu = get_mu(beta) mu_rad = math.radians(mu) lambda_rad = get_lambda(alpha_rad, beta) depth = D_center-(x-width_m/2)*math.sin(alpha_rad)*math.sin(mu_rad) return depth # 覆盖宽度(基于位置 x)(核心) def coverage_width(x, beta): depth = get_depth(x,beta) # 在中心点计算深度 # 获取μ角λ角 mu = get_mu(beta) mu_rad = math.radians(mu) lambda_rad = get_lambda(alpha_rad, beta) angle1, angle2 = get_angles(theta_rad, lambda_rad) sin_half_theta = math.sin(theta_rad / 2) W_1 = depth * sin_half_theta / math.sin(angle1) W_2 = depth * sin_half_theta / math.sin(angle2) return W_1 + W_2 def calculate_next_position(x_curr, x_prev, overlap_rate=0.1): """计算下一条测线位置""" D_n = D_center - x_curr * math.tan(alpha_rad) D_n_minus_1 = D_center - x_prev * math.tan(alpha_rad) angle1 = math.pi/2 - alpha_rad - theta_rad/2 angle2 = math.pi/2 + alpha_rad - theta_rad/2 AC_EF = (D_n * math.sin(theta_rad/2) / math.cos(angle1) + D_n_minus_1 * math.sin(theta_rad/2) / math.cos(angle2)) spacing = (1 - overlap_rate) * AC_EF * math.cos(alpha_rad) return x_curr - spacing def design_survey_lines(): survey_lines = [] # 第一条测线:最东侧,覆盖东边界 x_1 = width_m / 2 # 东边界位置 D_1 = D_center - x_1 * math.tan(alpha_rad) W_1 = coverage_width(x_1, beta=90) survey_lines.append({"序号": 1, "x坐标": x_1, "水深": D_1, "覆盖宽度": W_1}) current_x = x_1 k = 2 while True: # 计算下一条测线 x_prev = survey_lines[-1]["x坐标"] x_next = calculate_next_position(current_x, x_prev, overlap_rate=0.1) D_next = D_center - x_next * math.tan(alpha_rad) W_next = coverage_width(x_next, beta=90) survey_lines.append({"序号": k, "x坐标": x_next, "水深": D_next, "覆盖宽度": W_next}) current_x = x_next k += 1 # 判断是否覆盖西侧边界 coverage_left = max(D_next * math.sin(theta_rad/2) / math.cos(math.pi/2 - alpha_rad - theta_rad/2), 0) if (x_next - coverage_left) <= -width_m / 2: print(f"测线 {k-1} 已覆盖西侧边界,停止布设") break if x_next <= -width_m / 2: print(f"测线 {k-1} x={x_next} 超出西边界,停止布设") break # 确保最后一条测线覆盖西侧边界 last_line = survey_lines[-1] x_last = last_line["x坐标"] D_last = D_center - x_last * math.tan(alpha_rad) coverage_left = D_last * math.sin(theta_rad/2) / math.cos(math.pi/2 - alpha_rad - theta_rad/2) if (x_last - coverage_left) > -width_m / 2: x_final = -width_m / 2 D_final = D_center - x_final * math.tan(alpha_rad) W_final = coverage_width(x_final, beta=90) survey_lines.append({"序号": k, "x坐标": x_final, "水深": D_final, "覆盖宽度": W_final}) # 按照从东向西排序 survey_lines.sort(key=lambda x: x["x坐标"], reverse=True) for i, line in enumerate(survey_lines): line["序号"] = i + 1 return survey_lines # 执行测线布设 survey_lines = design_survey_lines() # 打印结果 total_lines = len(survey_lines) total_length = total_lines * length_n_mile average_spacing = np.mean([survey_lines[i]["x坐标"] - survey_lines[i+1]["x坐标"] for i in range(len(survey_lines)-1)]) / 1852 print(f"=== 测线设计方案 ===") print(f"共需布设 {total_lines} 条测线") print(f"总测线长度: {total_length:.2f} 海里") print(f"平均测线间距: {average_spacing:.2f} 海里") print("\n测线坐标:") for line in survey_lines: print(f"测线 {line['序号']}: x={line['x坐标']/1852:.2f} 海里, 水深={line['水深']:.2f} 米") # 保存到Excel df = pd.DataFrame(survey_lines) df.to_excel("result3.xlsx", index=False) # 可视化测线分布 xs = [line["x坐标"]/1852 for line in survey_lines] ys = [0]*len(xs) plt.figure(figsize=(10, 1)) plt.scatter(xs, ys, color='blue', zorder=2) plt.axvline(x=0, color='red', linestyle='--', label='中心线') plt.axvline(x=-2, color='gray', linestyle='--', label='西侧边界') plt.axvline(x=2, color='gray', linestyle='--', label='东侧边界') plt.yticks([]) plt.xlabel("测线位置 (海里)") plt.title("测线分布图") plt.legend() plt.grid(True) plt.tight_layout() plt.show()
最新发布
08-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值