Randomize Statement

J

Jeff Francis

Can you help! I'm totally new to Visual Basic save that I
have prepared Macro's within Word to enable reformatting
of documents created by my business software.

To assist my Son I am trying to prepare a random number
sequence so that a probability study can be made for
rolling a single dice.

What I need is a straightforward VB programme/Macro that
is going to be able to produce a random sequence of
numbers between 1 and 6. I would like the programme to be
adaptable to specify the length of the number sequence.

In VB help I have found the 'Randomize Statement' which
appears to be what I want but I have no idea of the
correct syntax to produce the programme.

If no-one can help I'll be up for the next few nights
rolling a dice by hand thousands of times over! There's
got to be an easier way!

Jeff
 
S

Steve Wylie

Hello Jeff

This response has absolutely nothing to do with Word, macros or VBA at all,
but if you have Excel on your PC, you can go into a cell and in the formula
bar type:

=RAND()*5+1

as a formula for that cell, then copy the formula down the required number
of cells (the 'length' of your sequence) and you'll get a load of random
numbers between 1 and 6.

Steve
 
S

Steve Wylie

.... perhaps I should mention when I say "type" in the formula bar, I mean
select the RAND function from the drop down list to the left of the formula
bar. Typing it in won't work.

Steve
 
M

martinique

There are two statements in VB/VBA dealing with random numbers.

Randomize() seeds the random number generator. You call this function once
at the start of your code:

Randomize Timer
same number each time, you'll get the same sequence of random numbers.


To generate a random number, use Rnd(). This returns a pseudo-random number
between 0 and 1. To generate a random integer, use:

n = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

or in your case

n = int( 6 * Rnd + 1 )


VB's random number generator has a bad reputation, but it's certainly
adequate for your purpose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top