基于日期的Excel VBA种子

基于日期的Excel VBA种子,excel,vba,seed,seeding,random-seed,Excel,Vba,Seed,Seeding,Random Seed,我在做一个使用种子的程序。这个种子应该基于日期,所以每一天我都会得到一个新的值。每天多次使用该种子不应改变返回的值 这就是我的种子的样子: If MainSheet.Cells(1, 1) = Date Then todayValue = MainSheet.Cells(1, 2) Else Randomize todayValue = Int(50 * Rnd) MainSheet.Cells(1,1) = Date 'saves the

我在做一个使用种子的程序。这个种子应该基于日期,所以每一天我都会得到一个新的值。每天多次使用该种子不应改变返回的值

这就是我的种子的样子:

If MainSheet.Cells(1, 1) = Date Then   
    todayValue = MainSheet.Cells(1, 2)
Else    
    Randomize
    todayValue =  Int(50 * Rnd)
    MainSheet.Cells(1,1) = Date    'saves the date in Sheet
    MainSheet.Cells(1,2) = todayValue    'saves todayValue in Sheet
End If
随机化

今日价值=整数(50*Rnd)

现在,我如何根据日期确定种子? 有没有其他方法可以根据日期来制作种子

多谢各位

在运行代码之前,只需使用负值调用Rnd函数。为此,可以使用Date函数。在VBA中,日期值仅为long,表示自过去某个设定日期起的天数

这里有一个例子,告诉你如何做你想做的事

Sub rndSeedTest()
    MsgBox Date & " --> " & CLng(Date)
    Rnd -Date
    MsgBox Rnd & _
        vbNewLine & Rnd & _
        vbNewLine & Rnd & _
        vbNewLine & Rnd & _
        vbNewLine & Rnd
End Sub

如果你想在每次函数运行时得到相同的数字,那么这不是一个随机数,你必须创建一些看起来随机的东西,例如

Sub test()
    Dim todayValue As Long
    todayValue = Date
    todayValue = todayValue + 1538 ' To make it a little random
    MsgBox todayValue
End Sub

我找到了一种方法,我只需将日期和当天的值保存在工作表的某个单元格中。我的代码是这样的:

If MainSheet.Cells(1, 1) = Date Then   
    todayValue = MainSheet.Cells(1, 2)
Else    
    Randomize
    todayValue =  Int(50 * Rnd)
    MainSheet.Cells(1,1) = Date    'saves the date in Sheet
    MainSheet.Cells(1,2) = todayValue    'saves todayValue in Sheet
End If

到目前为止你能写你的代码吗?所以我们可以更好地帮助你。。。