+ Reply to Thread
Results 1 to 7 of 7

Creating sinle random letter - code not working

  1. #1
    Forum Contributor
    Join Date
    01-18-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    127

    Creating sinle random letter - code not working

    Hi all,

    I've found the following that will create a random letter

    Please Login or Register  to view this content.
    However, whenever I run this it always creates the same sequence of randomisation - 'S' 'O' 'P' 'I' etc.

    My code is only run once each time, so I'm always getting S!

    How do I really get true randomisation of a letter?
    Last edited by Julesdude; 02-27-2012 at 09:45 AM.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Creating sinle random letter - code not working

    Before you call the Rnd function add the line of code:

    Please Login or Register  to view this content.
    To set the seed for the random number generator.

  3. #3
    Forum Contributor
    Join Date
    01-18-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Creating sinle random letter - code not working

    Lovely thank you Andrew.

    It works, however, I've noticed that occassionally it is generating the [ character. What am I doing wrong?

    Also, do I need to use the statement Randomise Timer each time I'm about to perform the RND command?

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Creating sinle random letter - code not working

    The "[" character is ASCII code 91, so you're overshooting your intended range by 1. This is because the CInt function rounds to get an integer value, so it can round the result of Rnd() * 26 up to 26, which you don't want.

    Try using:

    Please Login or Register  to view this content.
    As for the randomize function - generating random numbers is very tricky for a computer, so what the Rnd function gives you is the next number in a mathematical sequence defined by some complex formula. The initial value that is fed into this formula (the seed) can be set using the Randomize function, and using the timer function (which returns fractional number of seconds that have elapsed since midnight) makes it unlikely that the seed will be set to the same value every time the code is run.

    Normally you'd just call randomize at the start of a function or sub using Rnd, but if you'd like to call it more often then that's up to you.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Creating sinle random letter - code not working

    Yes, you need to randomize at the start.

    I also see the occasional [ bracket with your code. I use this slight variation and I don't get that:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Forum Contributor
    Join Date
    01-18-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    127

    Re: Creating sinle random letter - code not working

    Thanks Andrew. It works perfectly. And thanks for explaining how randomization is made. It's all a lot clearer to me now!

    Jerry thanks also.

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Creating sinle random letter - code not working

    or

    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)

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