-1

I'm working on a programm where I use a seed. This seed should be based on the date, so every single day i get a new value. Using the seed several times a day should not change the value getting back.

This is what my seed looks like:

Randomize

todayValue = Int(50 * Rnd)

Now, how do I base a seed on the date? Is there maybe another way to have a seed based on the date?

Thank you very much!

Darren Bartrup-Cook
  • 18,362
  • 1
  • 23
  • 45
J.schmidt
  • 721
  • 5
  • 27

3 Answers3

0

See this post on getting seeded random values in VBA.

All you need to do is call the Rnd function with a negative value, prior to running your code. To do that, you can use the Date function. In VBA, Date values are just Longs, representing the number of days since some set date in the past.

Here's an example of how you could do what you want.

Sub rndSeedTest()
    MsgBox Date & " --> " & CLng(Date)
    Rnd -Date
    MsgBox Rnd & _
        vbNewLine & Rnd & _
        vbNewLine & Rnd & _
        vbNewLine & Rnd & _
        vbNewLine & Rnd
End Sub
Josh Eller
  • 2,045
  • 6
  • 11
0

If you want to get same number every time the function runs, then that is not a random number, you have to create something that looks random, for example

Sub test()
    Dim todayValue As Long
    todayValue = Date
    todayValue = todayValue + 1538 ' To make it a little random
    MsgBox todayValue
End Sub
usmanhaq
  • 1,527
  • 1
  • 6
  • 11
0

I found a way to do it, i just save the date and the Value of the day in a certain cell on my sheet. this is how my code looks like:

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
J.schmidt
  • 721
  • 5
  • 27