Rss & SiteMap

Foxtable(狐表) http://www.foxtable.com

新一代数据库软件,完美融合Access、Foxpro、Excel、vb.net之优势,人人都能掌握的快速软件开发工具!
共10 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:简单功能求助

1楼
dknt 发表于:2024/10/28 16:44:00
目的:随机生成N个小数,所有小数和等于1
功能很简单,但是过程很艰难,望大神赐教!!
2楼
有点蓝 发表于:2024/10/28 17:00:00
Dim sum As Decimal = 1
Dim lst As New List (Of Decimal)
Dim n As Integer = 10
For i As Integer = 1 To n - 1
    Dim d As Decimal = Rand.NextDouble()
'    Output.Show(d)
    Do While sum <= d
        d = d / 10
    Loop 
    lst.Add (d)
    sum = sum - d
Next
lst.Add (sum)
Output.Show (String.Join (vbcrlf, lst.ToArray))
3楼
dknt 发表于:2024/10/28 17:37:00
牛逼!!感谢!!!
4楼
dknt 发表于:2024/10/28 17:50:00
大神,有个问题,能排除随机为0的数吗,就是所有随机小数不能出现为0的情况
5楼
有点蓝 发表于:2024/10/28 19:46:00
判断一下,如果是0就重新取一个
6楼
dknt 发表于:2024/10/29 10:29:00
Dim sum As Decimal = 1
Dim lst As New List (Of Decimal)
Dim n As Integer = 10
For i As Integer = 1 To n - 1
    Dim d As Decimal = Rand.NextDouble()
    Do While sum <= d
    if d > 0.1 and d < 0.4 then
        d = d / 10
    Loop 
   end if
    lst.Add (d)
    sum = sum - d
Next
lst.Add (sum)
Output.Show (String.Join (vbcrlf, lst.ToArray))
大神,这代码加上条件后就直接死机了,求助啊
7楼
有点蓝 发表于:2024/10/29 10:32:00
Dim sum As Decimal = 1
Dim lst As New List (Of Decimal)
Dim n As Integer = 10
For i As Integer = 1 To n - 1
    Dim d As Decimal = Rand.Next(1000000, 3000000) / 1000000
'    Output.Show(d)
    Do While sum <= d
        d = d / 10
    Loop 
    lst.Add (d)
    sum = sum - d
Next
lst.Add (sum)
Output.Show (String.Join (vbcrlf, lst.ToArray))
8楼
dknt 发表于:2024/10/29 11:00:00
大神,这个代码写进按钮控件Click事件中报错啊
图片点击可在新窗口打开查看此主题相关图片如下:df15541e6e06403f6abaef2d1fbfa9b.png
图片点击可在新窗口打开查看
9楼
有点蓝 发表于:2024/10/29 11:04:00
根据错误提示,认真看7楼代码
10楼
dknt 发表于:2024/10/29 11:15:00
谢谢版主!
共10 条记录, 每页显示 10 条, 页签: [1]

Copyright © 2000 - 2018 foxtable.com Tel: 4000-810-820 粤ICP备11091905号

Powered By Dvbbs Version 8.3.0
Processed in .02734 s, 2 queries.