+ Reply to Thread
Results 1 to 9 of 9

Shuffle this array, or else, fix my randomisation...

  1. #1
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Shuffle this array, or else, fix my randomisation...

    I have a macro that creates an array, populates it with random numbers then dumps it into a sheet.

    Its quite large, and I'm seeing obvious patterns in the output, so doesn't look remotely random... I've added a test workbook, but depending on PC spec, might not run in a friendly manner... Be warned. Iv'e cut it down as small as I can to be able to see the results... my actual scrip populates 333x333 cells, this does 50x50

    My question is either...

    Can someone spot anything in my code that would make this more random

    OR

    Can I shuffle the array a few times after it's created, so add some more "randomness" to it?


    This code was created from tutorials and forum posts, I'm "OK" at vba, but quite novice.

    Please Login or Register  to view this content.
    Many thanks in advance for anyones time and help, regardless of whether it solves my issue.
    Attached Files Attached Files
    Last edited by Darth269; 11-21-2020 at 08:09 AM.

  2. #2
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Shuffle this array, or else, fix my randomisation...

    I've added an image of the output at my end, to show what I mean about it not looking random...

    Attachment 705244

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

    Re: Shuffle this array, or else, fix my randomisation...

    Yes, VBA using pseudo-random number generator, as described by Microsoft :

    Rnd function
    Returns a Single containing a pseudo-random number.
    https://docs.microsoft.com/en-us/off...p/rnd-function


    You can use a WinAPI call instead, like this :
    Please Login or Register  to view this content.
    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
    Valued Forum Contributor Hydraulics's Avatar
    Join Date
    07-15-2018
    Location
    Udine - Italy
    MS-Off Ver
    Office 365
    Posts
    370

    Re: Shuffle this array, or else, fix my randomisation...

    For the random number generator, you could use

    Please Login or Register  to view this content.
    which is much better than VBA Rnd() .

    To improve speed, change all the FALSE in the Vlookup of the conditional formatting to TRUE (every random number exists in the table).

    HTH,

    Francesco
    Aim high or don't even try.
    ---------------------------------
    If your question has been answered, don't forget to mark the thread as SOLVED.
    If you find an answer helpful, click on the star icon at the bottom of the post.

  5. #5
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Shuffle this array, or else, fix my randomisation...

    Quote Originally Posted by Hydraulics View Post
    For the random number generator, you could use

    Please Login or Register  to view this content.
    which is much better than VBA Rnd() .

    To improve speed, change all the FALSE in the Vlookup of the conditional formatting to TRUE (every random number exists in the table).

    HTH,

    Francesco
    Regarding the VLookups... Wow, It never even occurred to me, but it's made a huge difference, thanks. I'll compare the two answers given, and let you all know how I get on...

  6. #6
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Shuffle this array, or else, fix my randomisation...

    Quote Originally Posted by karedog View Post
    Yes, VBA using pseudo-random number generator, as described by Microsoft :

    Rnd function
    Returns a Single containing a pseudo-random number.
    https://docs.microsoft.com/en-us/off...p/rnd-function


    You can use a WinAPI call instead, like this :
    Please Login or Register  to view this content.
    Thanks for this, I'll give it a go, but I have one query regarding the line...

    i = Int(Abs(i) / 2147483647 * 12000) + 1

    the obvious difference between the rnd functions upper and lower bounds, could you explain the use of 2147483647... or link any documentation that exists for this function so I could look at it in more depth?

  7. #7
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Shuffle this array, or else, fix my randomisation...

    Quote Originally Posted by karedog View Post
    Yes, VBA using pseudo-random number generator, as described by Microsoft :

    Rnd function
    Returns a Single containing a pseudo-random number.
    https://docs.microsoft.com/en-us/off...p/rnd-function


    You can use a WinAPI call instead, like this :
    Please Login or Register  to view this content.
    I'm getting an error, due to the 64bit MS Office (I assume) any thoughts?

    Attachment 705273

  8. #8
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Shuffle this array, or else, fix my randomisation...

    A quick note here to say that I've only been able to test Hydraulics suggestion so far, due to the 64bit error i'm working on, but it is (in conjunction with the vlookup change) MUCH faster and more random than my previous. I'll wait a bit to see if anyone could advise further.

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

    Re: Shuffle this array, or else, fix my randomisation...

    Quote Originally Posted by Darth269 View Post
    Thanks for this, I'll give it a go, but I have one query regarding the line...

    i = Int(Abs(i) / 2147483647 * 12000) + 1

    the obvious difference between the rnd functions upper and lower bounds, could you explain the use of 2147483647... or link any documentation that exists for this function so I could look at it in more depth?
    You are welcome, thanks for the rep.points.

    The Microsoft documentation is here :
    https://docs.microsoft.com/en-us/win...i-rtlgenrandom

    We want to mimic the rnd function, which produce number between 0 and 1
    Since we pass a long variable to this function, the result number is of course between the "Long" variable scope.
    If you highlight the "Long" word in VBA then press F1, the help will show this :
    Long Data Type
    Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647. The type-declaration character for Long is the ampersand (&).
    So to convert this return value of Long variable, we use Abs(i) which will produce positive value only then divided by the maximum number of the Long datatype scope to get number between 0 and 1, just as the same with the rnd function.



    Quote Originally Posted by Darth269 View Post
    I'm getting an error, due to the 64bit MS Office (I assume) any thoughts?
    Change the function declaration to this (tested on Windows 8 with Excel 2010 64 bit) :
    Please Login or Register  to view this content.

+ 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. Smart Randomisation
    By archiplexus in forum Excel General
    Replies: 1
    Last Post: 11-14-2018, 01:12 PM
  2. [SOLVED] Shuffle array VBA
    By sigit21 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-18-2018, 05:47 AM
  3. [SOLVED] Improving array shuffle
    By Kyle123 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 09-17-2015, 09:25 AM
  4. [SOLVED] Weird randomisation issue with nested rand() functions
    By qdtruong in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-13-2015, 01:10 PM
  5. [SOLVED] Shuffle array (Scheduling)
    By hcyeap in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-22-2015, 08:59 AM
  6. Formula & Index randomisation
    By mr63249 in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 11-15-2008, 09:31 PM
  7. Shuffle Array
    By Rik Smith in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-24-2005, 12:05 PM

Tags for this Thread

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