+ Reply to Thread
Results 1 to 13 of 13

Radomize seconds in a timestamp over a undefined range.

  1. #1
    Forum Contributor
    Join Date
    09-11-2008
    Location
    uk
    MS-Off Ver
    2010; 2016
    Posts
    390

    Radomize seconds in a timestamp over a undefined range.

    All,

    I have a column of timestamps (c3: cx) in the following format (h:mm:ss AM/PM).

    I would simply like to randomize the seconds section of each stamp via a macro and simply cannot fathom it out...please help!!

    Regards,

    Skyping
    Last edited by skyping; 01-04-2015 at 12:04 PM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: Radomize seconds in a timestamp over a undefined range.

    try:
    Please Login or Register  to view this content.
    Best Regards,

    Kaper

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Radomize seconds in a timestamp over a undefined range.

    Hi,

    Try this code :

    Please Login or Register  to view this content.
    Regards
    Last edited by karedog; 01-04-2015 at 11:07 AM.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: Radomize seconds in a timestamp over a undefined range.

    hi karedog,
    it is not needed to initialize pseudo-random generator before each call of Rnd.
    Last edited by Kaper; 01-04-2015 at 11:24 AM. Reason: deleted info on days vs. seconds as it has been changed in the code

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Radomize seconds in a timestamp over a undefined range.

    Hi Kaper,

    It is just a good practice (but probably not needed for this case), as Microsoft says :

    Because the Random statement and the Rnd function start with a seed value and generate numbers that fall within a finite range, the results may be predictable by someone who knows the algorithm used to generate them.
    http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

    BTW, your code only change the seconds only to 0 or 1, is this expected ?

    Regards
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    09-11-2008
    Location
    uk
    MS-Off Ver
    2010; 2016
    Posts
    390

    Re: Radomize seconds in a timestamp over a undefined range.

    Thank you all for your input. I have used karedogs code, but appreciate both your help.

    SOLVED

  7. #7
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Radomize seconds in a timestamp over a undefined range.

    You are welcome, glad I can help.

    Regards

  8. #8
    Forum Contributor
    Join Date
    09-11-2008
    Location
    uk
    MS-Off Ver
    2010; 2016
    Posts
    390

    Re: Radomize seconds in a timestamp over a undefined range.

    Just one further question.....how would I randomize the last minute in the above scenario....e.g. h:mm:ss AM/PMh:mm:ss AM/PM goes to h:mX:ss AM/PM ?

    Thanks
    Skyping.

  9. #9
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Radomize seconds in a timestamp over a undefined range.

    You can use this code :

    Please Login or Register  to view this content.
    Regards

  10. #10
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: Radomize seconds in a timestamp over a undefined range.

    @karedog: "code only change the seconds only to 0 or 1, is this expected ?"
    So we have a tie here, because your first code (before editing) changed whole day :-D

    But it's not the main point to write. I'd like to discuss randomize a bit further.

    It is wise to use it, and you probably noticed, that I used it too, but outside of the loop.
    But Randomize without parameter uses (I think) system clock as a seed value. So data series generated with periodical initialisation would be "less randomly" distributed than initialized only once.

    Have a look into sample file - there are 10000 numbers generated with randomize before each rnd and 10000 just one randomize at the beginning.
    Have a look at the difference in frequency of different values generated by both approaches.
    I'm not statistician, but anyway densities (frequencies) of numbers generated with repeated randomize are at least surprisingly located.

    And it's not just one run result - call test procedure and next (different but again strange) set will be generated. If you examine only a small subsample (say 100) it's not clearly visible. but larger datasets are by no means berrer randomized by repeating randomize ;-).
    Attached Files Attached Files
    Last edited by Kaper; 01-07-2015 at 08:45 AM.

  11. #11
    Forum Contributor
    Join Date
    09-11-2008
    Location
    uk
    MS-Off Ver
    2010; 2016
    Posts
    390

    Re: Radomize seconds in a timestamp over a undefined range.

    Thank you once again Karedog...work great!!!

  12. #12
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Radomize seconds in a timestamp over a undefined range.

    Quote Originally Posted by skyping View Post
    Thank you once again Karedog...work great!!!
    You are welcome Skyping, many thanks for the rep. points too.


    Quote Originally Posted by Kaper View Post
    So we have a tie here, because your first code (before editing) changed whole day :-D
    Ok Kaper, it's a draw then mrgreen.gif

    But it's not the main point to write. I'd like to discuss randomize a bit further.

    It is wise to use it, and you probably noticed, that I used it too, but outside of the loop.
    But Randomize without parameter uses (I think) system clock as a seed value. So data series generated with periodical initialisation would be "less randomly" distributed than initialized only once.

    Have a look into sample file - there are 10000 numbers generated with randomize before each rnd and 10000 just one randomize at the beginning.
    Have a look at the difference in frequency of different values generated by both approaches.
    I'm not statistician, but anyway densities (frequencies) of numbers generated with repeated randomize are at least surprisingly located.

    And it's not just one run result - call test procedure and next (different but again strange) set will be generated. If you examine only a small subsample (say 100) it's not clearly visible. but larger datasets are by no means berrer randomized by repeating randomize ;-).
    Well, I'm not a statistic guy either, so I cannot comment about your work, whether the data is "more random" or "same random" or something else, sorry about this.

    Regards

  13. #13
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Radomize seconds in a timestamp over a undefined range.

    Quote Originally Posted by skyping View Post
    I have a column of timestamps (c3: cx) in the following format (h:mm:ss AM/PM). I would simply like to randomize the seconds section of each stamp via a macro
    Quote Originally Posted by skyping View Post
    how would I randomize the last minute in the above scenario....e.g. h:mm:ss AM/PMh:mm:ss AM/PM goes to h:mX:ss AM/PM ?
    You marked this thread as "solved", but none of the suggestions does what you ask. Try the following:

    Please Login or Register  to view this content.
    The code assumes the cells are formatted as you want.

    Quote Originally Posted by Kaper View Post
    cells(i,3) = cells(i,3) + rnd/24/60/60
    Since Rnd returns a non-integer between 0 and less than 1, this adds random fractional seconds less than 1 instead of whole seconds.

    Quote Originally Posted by karedog View Post
    cell.Value = cell.Value + (Rnd / 1440)
    Quote Originally Posted by karedog View Post
    cell.Value = cell.Value + (Int(Rnd * 10) / 1440)
    Although this adds fractional minutes (and less than 10 minutes in the second case), which are seconds, the result includes fractional seconds. For example, if Rnd returns 0.1234, Rnd/1440 adds about 7.404 seconds. I suspect "skyping" wants random whole seconds. The fractional seconds might screw up the way he uses the random times.

    Moreover, both methods add random time instead of just inserting random seconds. Consequently, they have the potential of changing the minute (minute "decade" in the second case) and hour, which does not seem to be "skyping's" intent.
    Last edited by joeu2004; 01-08-2015 at 08:10 PM. Reason: cosmetic

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro for timestamp with seconds for logging data
    By jimmerson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-05-2012, 06:14 PM
  2. Changing cellvalues in undefined range
    By MarMo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-03-2010, 01:13 PM
  3. Naming an undefined range
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-26-2010, 04:37 PM
  4. Range.Formula becomes error/undefined
    By mikeyfear in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-28-2008, 05:41 PM
  5. Macro to Remove "Seconds" From Timestamp
    By elite-rob in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-30-2008, 06:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1