如何在VB.Net中创建概率布尔值 [英] How Do I Create A Probability Boolean in VB.Net

查看:102
本文介绍了如何在VB.Net中创建概率布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这很简单,我只是不知道如何在bing中搜索它。这个想法是用户单击一个按钮,变量为真的几率为百分之一,否则为假。如果它返回true,那么我需要它在数据库中搜索以查找使用txtbox文本中显示的drop组列出的所有项目,并显示该列表中的随机项目。



大部分听起来都很简单,但我唯一不知道的是如何获得启动它的真或假。任何人都可以给我一个示例代码,有60%的机会返回true吗?



如果示例代码超出了一些简单的命令,那么我需要为每个代码解释一下一套命令。

I have a feeling this is something very simple and I just don't know how to phrase it when searching in bing. The idea is that the user clicks a button and there is a percent chance of a variable being true, else it is false. If it returns true then I need it to search in a database to find all items listed with the drop group shown in the text of a txtbox and display a random item from that list.

Most of this sounds pretty easy but the only thing I have no clue on is how to get the true or false that starts it all. Can anybody give me an example code that has a 60% chance of returning true?

If the example code goes beyond a few simple commands then I need an explanation for each set of commands.

推荐答案

我自己找到了一个解决方案,所以我会留在这里以防其他人需要知道。我创建了一个生成0到101(或1-100)之间的随机数的函数,如果生成的值低于概率(即如果概率是60%,随机生成的数字是30)那么它运行如果随机生成的数字高于概率,则脚本为true,然后运行脚本为false。虽然它不是我想要的,但它确实有效。



Well I found a solution on my own so I will leave it here in case someone else needs to know. I created a function that generates a random number between 0 and 101 (or 1-100 inclusive) and if the value generated is lower than the probability (i.e. if the probability is 60% and the randomly generated number is 30) then it runs the script for true, if the randomly generated number is higher than the probability, then runs the script for false. While it's not exactly what I was looking for, it does work.

Public Class Form1

    Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
        ' by making Generator static, we preserve the same instance '
        ' (i.e., do not create new instances with the same seed over and over) '
        ' between calls '
        Static Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function

    Private Sub BoolBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoolBtn.Click

        If GetRandom(0, 101) < BoolNum.Value Then
            MsgBox("True")
        Else
            MsgBox("False")
        End If

    End Sub
End Class


这篇关于如何在VB.Net中创建概率布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆