RAND function

  • Thread starter Thread starter Jac Tremblay
  • Start date Start date
J

Jac Tremblay

Hi!

I've got a little problem to solve on a worksheet without
using VBA functions (it would be too easy then!).

I want to use only worksheet functions to find 3 different
numbers from 1 to 6 (let's say). The first number may be
found with the formula:

=INT(RAND()*NbPers)+1

I want the second one to be different than the first one.

And I want the 3rd one to be different than the first two
ones.

I have found part of the answer from Mr. "Larry
Griffith" "Modeste" <[email protected]> but it does not
work to my satisfaction. I know it is possible but wander
how.

Can someone help me, please?

Thank you.
 
...
...
I want to use only worksheet functions to find 3 different
numbers from 1 to 6 (let's say). The first number may be
found with the formula:

=INT(RAND()*NbPers)+1

I want the second one to be different than the first one.

And I want the 3rd one to be different than the first two
ones.
...

You need to exclude the the first number from the possible second number results
and both the first and second numbers from the possible third number results.
This is easier if you draw from a population of 1 to 6, so I'll define the name
Pop referring to =ROW(INDIRECT("1:"&NbPers)). Then try

first number: [in A3]
=LARGE(Pop,INT(1+N*RAND()))

second number: [array formula - in A4]
=LARGE(IF(COUNTIF(A$3:A3,Sample)=0,Sample),INT(1+(N-ROW()+ROW(A$3))*RAND()))

third number: [array formula - in A5]
=LARGE(IF(COUNTIF(A$3:A4,Sample)=0,Sample),INT(1+(N-ROW()+ROW(A$3))*RAND()))

Note that you could have filled A4 down into A5. If all values in Pop appear
only once, this is the most general approach to generating samples without
replacement.
 
Myrna Larson said:
Question, Harlan:

To what does "Sample" refer in your formulas? The sample size,
i.e. 3 in the OP's case?
....

It should have been Pop rather than Sample. I pasted formulas into my
response, changed my mind about the names used, and didn't fix the formulas.

They should be

Define the name Pop referring to =ROW(INDIRECT("1:"&NbPers))

first number: [in A3]
=LARGE(Pop,INT(1+NbPers*RAND()))

second number: [array formula - in A4]
=LARGE(IF(COUNTIF(A$3:A3,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))

third number: [array formula - in A5]
=LARGE(IF(COUNTIF(A$3:A4,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))
 
Thanks for the correction. I can't even count the number of times I've done exactly the same
thing!

Myrna Larson said:
Question, Harlan:

To what does "Sample" refer in your formulas? The sample size,
i.e. 3 in the OP's case?
...

It should have been Pop rather than Sample. I pasted formulas into my
response, changed my mind about the names used, and didn't fix the formulas.

They should be

Define the name Pop referring to =ROW(INDIRECT("1:"&NbPers))

first number: [in A3]
=LARGE(Pop,INT(1+NbPers*RAND()))

second number: [array formula - in A4]
=LARGE(IF(COUNTIF(A$3:A3,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))

third number: [array formula - in A5]
=LARGE(IF(COUNTIF(A$3:A4,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))
 
Mr. Harlan, thank you for your time but I can't see how
exactly you do it?

Note: I work with Excel 2000 in french (so I got to
translate the function names before I publish my formulas
on the web).

Here are my questions:

Question 1:

When you say: "This is easier if you draw from a
population of 1 to 6, so I'll define the name Pop
referring to =ROW(INDIRECT("1:"&NbPers)).", what do you
mean?

Do you enter the numbers 1 to 6 in some cells and name the
range "Pop"? Then, what if I have 1000 persons? Or if I
have to change the number of persons constantly?

What is this formula supposed to be for? It always returns
the number 1 anyway (maximum NbPers or 6 here).

Question 2:

In the formula "=LARGE(IF(COUNTIF(A$3:A3,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))", why do you
use "A$3"? Why not "$A$3"?

Question 3:

What do you mean by: " [array formula - in A4]"?

Question 4:

What does OP mean?

Thank you again for your time.

Jac T.
-----Original Message-----
Myrna Larson said:
Question, Harlan:

To what does "Sample" refer in your formulas? The sample size,
i.e. 3 in the OP's case?
....

It should have been Pop rather than Sample. I pasted formulas into my
response, changed my mind about the names used, and didn't fix the formulas.

They should be

Define the name Pop referring to =ROW(INDIRECT ("1:"&NbPers))

first number: [in A3]
=LARGE(Pop,INT(1+NbPers*RAND()))

second number: [array formula - in A4]
=LARGE(IF(COUNTIF(A$3:A3,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))

third number: [array formula - in A5]
=LARGE(IF(COUNTIF(A$3:A4,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))


.
 
Mr. Harlan, thank you for your time but I can't see how
exactly you do it?
...

Drop the Mr. or use my last name, M. Jac.
Question 1:

When you say: "This is easier if you draw from a
population of 1 to 6, so I'll define the name Pop
referring to =ROW(INDIRECT("1:"&NbPers)).", what do you
mean?

In English versions, there's an Insert entry in the main Excel menu, located in
4th position after File, Edit and View entries and before Format, Tools, Data,
Window and Help. Clicking on Insert displays its menu, and part way down there's
an entry for Name, which displays a submenu, and on that submenu there's a
Define... entry. So Insert > Name > Define... displays a dialog titled Define
Name. Enter (without the single quotes) 'Pop' in the topmost field (just beloe
the label 'Names in workbook:'), and '=ROW(INDIRECT("1:"&NbPers))' in the
bottommost field (just below the label 'Refers to:') and click the OK button.

I have no idea what the corresponding menu entries may be called in French
versions.
Do you enter the numbers 1 to 6 in some cells and name the
range "Pop"? Then, what if I have 1000 persons? Or if I
have to change the number of persons constantly?

I was assuming NbPers was also a defined name or a named cell. If so, then when
you change NbPers, Pop will change accordingly. So if NbPers began as 6, Pop
would evaluate to {1;2;3;4;5;6}. If you then change NbPers to 1000, Pop would
change to {1;2;3;4;...;999;1000}. That's why I made Pop a defined name depending
on the value of NbPers.
What is this formula supposed to be for? It always returns
the number 1 anyway (maximum NbPers or 6 here).

The formula evaluates to an array. Like all arrays, if you enter =Pop into a
single cell, you see only the first entry in Pop, which is always 1. If you type
=Pop in a cell and press [F9] rather than [Enter], you'll see it evaluates to
={1;2;3;4;5;6} (actually, if your version of Excel uses semicolons as list
separator, you may see a different character between the digits, but you
wouldn't see just =1).

Question 2:

In the formula "=LARGE(IF(COUNTIF(A$3:A3,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))", why do you
use "A$3"? Why not "$A$3"?

Only the row is important. ROW(A$3) is *ALWAYS* the same as ROW($X$3), ROW(X$3),
ROW($X3), ROW(X3) and even ROW(A3), ROW($A3) and ROW($A$3). Nothing is gained
using ROW($A$3) instead of ROW(A$3), but nothing would be lost either. The only
important thing is that the column portion of the reference, whether absolute or
relative, refer to the column in which these formulas were entered. Then these
formulas would be immune to inserted or deleted columns, excluding deletion of
the column in which they appear.

Question 3:

What do you mean by: " [array formula - in A4]"?

You'd be better off reading about array formulas in online help. They're
frequently mentioned in the English Excel newsgroups, and I'd be very much
surprised if they weren't mentioned as frequently in French Excel newsgroups.

Array formulas are entered differently than nonarray/standard formulas. Instead
of just pressing [Enter], you must hold down [Ctrl] and [Shift] keys (one of
each is sufficient) before pressing [Enter]. If done correctly, the formula bar
displays the formula insude curly braces.
Question 4:

What does OP mean?
...

Original poster. In the context of this thread, you.
 
Hi, Harlan,

Sorry for the "Mr."... You're probably way younger than I
am. Can't be any other way...

Again, I will have to test and try this out and then
comment it all. At first glance, it sound good.

Thank you for your time.

Jac T.

-----Original Message-----
Mr. Harlan, thank you for your time but I can't see how
exactly you do it?
...

Drop the Mr. or use my last name, M. Jac.
Question 1:

When you say: "This is easier if you draw from a
population of 1 to 6, so I'll define the name Pop
referring to =ROW(INDIRECT("1:"&NbPers)).", what do you
mean?

In English versions, there's an Insert entry in the main Excel menu, located in
4th position after File, Edit and View entries and before Format, Tools, Data,
Window and Help. Clicking on Insert displays its menu, and part way down there's
an entry for Name, which displays a submenu, and on that submenu there's a
Define... entry. So Insert > Name > Define... displays a dialog titled Define
Name. Enter (without the single quotes) 'Pop' in the topmost field (just beloe
the label 'Names in workbook:'), and '=ROW(INDIRECT ("1:"&NbPers))' in the
bottommost field (just below the label 'Refers to:') and click the OK button.

I have no idea what the corresponding menu entries may be called in French
versions.
Do you enter the numbers 1 to 6 in some cells and name the
range "Pop"? Then, what if I have 1000 persons? Or if I
have to change the number of persons constantly?

I was assuming NbPers was also a defined name or a named cell. If so, then when
you change NbPers, Pop will change accordingly. So if NbPers began as 6, Pop
would evaluate to {1;2;3;4;5;6}. If you then change NbPers to 1000, Pop would
change to {1;2;3;4;...;999;1000}. That's why I made Pop a defined name depending
on the value of NbPers.
What is this formula supposed to be for? It always returns
the number 1 anyway (maximum NbPers or 6 here).

The formula evaluates to an array. Like all arrays, if you enter =Pop into a
single cell, you see only the first entry in Pop, which is always 1. If you type
=Pop in a cell and press [F9] rather than [Enter], you'll see it evaluates to
={1;2;3;4;5;6} (actually, if your version of Excel uses semicolons as list
separator, you may see a different character between the digits, but you
wouldn't see just =1).

Question 2:

In the formula "=LARGE(IF(COUNTIF(A$3:A3,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))", why do you
use "A$3"? Why not "$A$3"?

Only the row is important. ROW(A$3) is *ALWAYS* the same as ROW($X$3), ROW(X$3),
ROW($X3), ROW(X3) and even ROW(A3), ROW($A3) and ROW ($A$3). Nothing is gained
using ROW($A$3) instead of ROW(A$3), but nothing would be lost either. The only
important thing is that the column portion of the reference, whether absolute or
relative, refer to the column in which these formulas were entered. Then these
formulas would be immune to inserted or deleted columns, excluding deletion of
the column in which they appear.

Question 3:

What do you mean by: " [array formula - in A4]"?

You'd be better off reading about array formulas in online help. They're
frequently mentioned in the English Excel newsgroups, and I'd be very much
surprised if they weren't mentioned as frequently in French Excel newsgroups.

Array formulas are entered differently than
nonarray/standard formulas. Instead
of just pressing [Enter], you must hold down [Ctrl] and [Shift] keys (one of
each is sufficient) before pressing [Enter]. If done correctly, the formula bar
displays the formula insude curly braces.
Question 4:

What does OP mean?
...

Original poster. In the context of this thread, you.

--
Never attach files.
Snip unnecessary quoted text.
Never multipost (though crossposting is usually OK).
Don't change subject lines because it corrupts Google newsgroup archives.
.
 
Salut Daniel M.,

Je n'ai pas de problèmes avec l'anglais, mais il me fera
plaisir de consulter votre site en français. Je suis
content de savoir que c'est possible de travailler en
français tout le temps...

Merci pour le tuyau.

Au plaisir.

Jac Tremblay
-----Original Message-----
Salut Jacques,

Si tu as des difficultés à bien interpréter les réponses ou les formules en
anglais, il serait approprié que tu viennes faire un tour sur le groupe de
discussion excel francophone :
news://microsoft.public.fr.excel

Les formules et explicitations te seront données en français.

Il y a un utilitaire (j'en suis l'auteur) là-dessus sur
www.excelabo.net qui
 
-----Original Message-----
Salut Jacques,

Si tu as des difficultés à bien interpréter les réponses ou les formules en
anglais, il serait approprié que tu viennes faire un tour sur le groupe de
discussion excel francophone :
news://microsoft.public.fr.excel

Les formules et explicitations te seront données en français.

Il y a un utilitaire (j'en suis l'auteur) là-dessus sur
www.excelabo.net qui
fait les traductions automatiques entre les formules.


Menu Insère/Nom/Définir


Harlan t'a répondu.

Salutations,

Daniel M.

.
Salut Daniel M.,

C'est très bien mais j'ai un message d'erreur quand
j'essaie d'accéder à ce site. Je ne suis pas abonné à
aucun groupe et je n'ai pas Outlook Express et je n'y
tiens pas...

J'ai consulté le site Excelabo et trouvé ça très
intéressant.

Merci pour le tuyau.

Jac Tremblay
 
Salut Jac,

Bizarre!
Je ne connais malheureusement pas l'interface CDO.
Le groupe de discussion microsoft.public.fr.excel existe bel et bien et est
très actif.

Tu peux peut-être y accéder plus facilement par microsoft france (CDO),
c'est juste une suggestion.
En tout cas, si jamais tu y arrives, sache qu'en saluant les gens et en les
remerciant à l'avance, l'aide aux questions est fournie assez rapidement.

Salutations,

Daniel M.
Salut Daniel M.,
C'est très bien mais j'ai un message d'erreur quand
j'essaie d'accéder à ce site. Je ne suis pas abonné à
aucun groupe et je n'ai pas Outlook Express et je n'y
tiens pas...
J'ai consulté le site Excelabo et trouvé ça très
intéressant.
 
-----Original Message-----
Hi, Harlan,

Sorry for the "Mr."... You're probably way younger than I
am. Can't be any other way...

Again, I will have to test and try this out and then
comment it all. At first glance, it sound good.

Thank you for your time.

Jac T.

-----Original Message-----
...
...

Drop the Mr. or use my last name, M. Jac.


In English versions, there's an Insert entry in the main Excel menu, located in
4th position after File, Edit and View entries and
before
Format, Tools, Data,
Window and Help. Clicking on Insert displays its menu, and part way down there's
an entry for Name, which displays a submenu, and on that submenu there's a
Define... entry. So Insert > Name > Define... displays a dialog titled Define
Name. Enter (without the single quotes) 'Pop' in the topmost field (just beloe
the label 'Names in workbook:'), and '=ROW(INDIRECT ("1:"&NbPers))' in the
bottommost field (just below the label 'Refers to:') and click the OK button.

I have no idea what the corresponding menu entries may
be
called in French
versions.


I was assuming NbPers was also a defined name or a named cell. If so, then when
you change NbPers, Pop will change accordingly. So if NbPers began as 6, Pop
would evaluate to {1;2;3;4;5;6}. If you then change NbPers to 1000, Pop would
change to {1;2;3;4;...;999;1000}. That's why I made Pop
a
defined name depending
on the value of NbPers.
What is this formula supposed to be for? It always returns
the number 1 anyway (maximum NbPers or 6 here).

The formula evaluates to an array. Like all arrays, if you enter =Pop into a
single cell, you see only the first entry in Pop, which is always 1. If you type
=Pop in a cell and press [F9] rather than [Enter],
you'll
see it evaluates to
={1;2;3;4;5;6} (actually, if your version of Excel uses semicolons as list
separator, you may see a different character between the digits, but you
wouldn't see just =1).



Only the row is important. ROW(A$3) is *ALWAYS* the same as ROW($X$3), ROW(X$3),
ROW($X3), ROW(X3) and even ROW(A3), ROW($A3) and ROW ($A$3). Nothing is gained
using ROW($A$3) instead of ROW(A$3), but nothing would
be
lost either. The only
important thing is that the column portion of the reference, whether absolute or
relative, refer to the column in which these formulas were entered. Then these
formulas would be immune to inserted or deleted columns, excluding deletion of
the column in which they appear.

Question 3:

What do you mean by: " [array formula - in A4]"?

You'd be better off reading about array formulas in online help. They're
frequently mentioned in the English Excel newsgroups,
and
I'd be very much
surprised if they weren't mentioned as frequently in French Excel newsgroups.

Array formulas are entered differently than
nonarray/standard formulas. Instead
of just pressing [Enter], you must hold down [Ctrl] and [Shift] keys (one of
each is sufficient) before pressing [Enter]. If done correctly, the formula bar
displays the formula insude curly braces.
Question 4:

What does OP mean?
...

Original poster. In the context of this thread, you.

--
Never attach files.
Snip unnecessary quoted text.
Never multipost (though crossposting is usually OK).
Don't change subject lines because it corrupts Google newsgroup archives.
.
.
Hi Harlan,

Your solution does not produce different numbers all the
times even though I have followed your instructions to the
letter. When I press F9 to produce new results, the
numbers are often the same, especially if I use only 3
entries (test data). Maybe I did not get the whole thing
right, but I tested it all I could.

But, even though I did not get a convenient solution to my
problem with your suggestion, your comments have provided
me with a little more experience with Excel. For example,
I didn't know that a named cell could take a formula such
as "=ROW(INDIRECT("1:"&NbPers))". I thought one could only
enter an address in that field.

Plus, I learned the use of F9 with an array formula. It is
a great discovery for me. I Thought it was only for
recalculating the worksheet. Thanks to you.

I have found some other way of solving this problem. It is
the simplest solution to date. Tell me what you think of
it.

Type in A3:An the formula "=RAND()".
Then in B3:Bn, "=RANK(A3;A:A)".
And you got your numbers, always different no matter how
many you use.

I thank you for your precious time.

Jac Tremblay
 
Hi Harlan,

Your solution does not produce different numbers all the
times even though I have followed your instructions to the
letter. When I press F9 to produce new results, the
numbers are often the same, especially if I use only 3
entries (test data). Maybe I did not get the whole thing
right, but I tested it all I could.

But, even though I did not get a convenient solution to my
problem with your suggestion, your comments have provided
me with a little more experience with Excel. For example,
I didn't know that a named cell could take a formula such
as "=ROW(INDIRECT("1:"&NbPers))". I thought one could only
enter an address in that field.

Plus, I learned the use of F9 with an array formula. It is
a great discovery for me. I Thought it was only for
recalculating the worksheet. Thanks to you.

I have found some other way of solving this problem. It is
the simplest solution to date. Tell me what you think of
it.

Type in A3:An the formula "=RAND()".
Then in B3:Bn, "=RANK(A3;A:A)".
And you got your numbers, always different no matter how
many you use.

I thank you for your precious time.

Jac Tremblay
-----Original Message-----
Mr. Harlan, thank you for your time but I can't see how
exactly you do it?
...

Drop the Mr. or use my last name, M. Jac.
Question 1:

When you say: "This is easier if you draw from a
population of 1 to 6, so I'll define the name Pop
referring to =ROW(INDIRECT("1:"&NbPers)).", what do you
mean?

In English versions, there's an Insert entry in the main Excel menu, located in
4th position after File, Edit and View entries and before Format, Tools, Data,
Window and Help. Clicking on Insert displays its menu, and part way down there's
an entry for Name, which displays a submenu, and on that submenu there's a
Define... entry. So Insert > Name > Define... displays a dialog titled Define
Name. Enter (without the single quotes) 'Pop' in the topmost field (just beloe
the label 'Names in workbook:'), and '=ROW(INDIRECT ("1:"&NbPers))' in the
bottommost field (just below the label 'Refers to:') and click the OK button.

I have no idea what the corresponding menu entries may be called in French
versions.
Do you enter the numbers 1 to 6 in some cells and name the
range "Pop"? Then, what if I have 1000 persons? Or if I
have to change the number of persons constantly?

I was assuming NbPers was also a defined name or a named cell. If so, then when
you change NbPers, Pop will change accordingly. So if NbPers began as 6, Pop
would evaluate to {1;2;3;4;5;6}. If you then change NbPers to 1000, Pop would
change to {1;2;3;4;...;999;1000}. That's why I made Pop a defined name depending
on the value of NbPers.
What is this formula supposed to be for? It always returns
the number 1 anyway (maximum NbPers or 6 here).

The formula evaluates to an array. Like all arrays, if you enter =Pop into a
single cell, you see only the first entry in Pop, which is always 1. If you type
=Pop in a cell and press [F9] rather than [Enter], you'll see it evaluates to
={1;2;3;4;5;6} (actually, if your version of Excel uses semicolons as list
separator, you may see a different character between the digits, but you
wouldn't see just =1).

Question 2:

In the formula "=LARGE(IF(COUNTIF(A$3:A3,Pop)=0,Pop),
INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))", why do you
use "A$3"? Why not "$A$3"?

Only the row is important. ROW(A$3) is *ALWAYS* the same as ROW($X$3), ROW(X$3),
ROW($X3), ROW(X3) and even ROW(A3), ROW($A3) and ROW ($A$3). Nothing is gained
using ROW($A$3) instead of ROW(A$3), but nothing would be lost either. The only
important thing is that the column portion of the reference, whether absolute or
relative, refer to the column in which these formulas were entered. Then these
formulas would be immune to inserted or deleted columns, excluding deletion of
the column in which they appear.

Question 3:

What do you mean by: " [array formula - in A4]"?

You'd be better off reading about array formulas in online help. They're
frequently mentioned in the English Excel newsgroups, and I'd be very much
surprised if they weren't mentioned as frequently in French Excel newsgroups.

Array formulas are entered differently than
nonarray/standard formulas. Instead
of just pressing [Enter], you must hold down [Ctrl] and [Shift] keys (one of
each is sufficient) before pressing [Enter]. If done correctly, the formula bar
displays the formula insude curly braces.
Question 4:

What does OP mean?
...

Original poster. In the context of this thread, you.

--
Never attach files.
Snip unnecessary quoted text.
Never multipost (though crossposting is usually OK).
Don't change subject lines because it corrupts Google newsgroup archives.
.
 
Your solution does not produce different numbers all the
times even though I have followed your instructions to the
letter. When I press F9 to produce new results, the
numbers are often the same, especially if I use only 3
entries (test data). Maybe I did not get the whole thing
right, but I tested it all I could.
...

I've tested this pretty thoroughly. In my workbook, with the second and
subsequent formulas entered as *array* formulas, the formulas do work as I say
they do. Are you sure you entered the

=LARGE(IF(COUNTIF(A$3:A3,Pop)=0,Pop),INT(1+(NbPers-ROW()+ROW(A$3))*RAND()))

formula in cell A4 with the first formula entered in cell A3? If so, then the
expression IF(COUNTIF(A$3:A3,Pop)=0,Pop) evaluates to an array in which the
value in Pop equal to the value in A3 becomes zero while the other values in Pop
remain as-is. You can't get repeats, but if I were screwing up the INT(..) term,
you could get zeros.

Are you entering the formulas AS I WROTE THEM into cells A3, A4 and A5? If not,
are you adjusting the formulas to reflect the cells into which you are entering
them?
 
Back
Top