MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 17th, 2002, 07:35 AM   #1
abz2max
New Member
 
Join Date: Apr 2002
Posts: 19
Default

I need to create a list of random numbers, that do not repeat each other. The range of numbers i will need will be 1 to n. n = any number>1. how can i create a macro to do this.
abz2max is offline   Reply With Quote
Old Apr 17th, 2002, 10:08 AM   #2
eliW
Board Regular
 
eliW's Avatar
 
Join Date: Mar 2002
Posts: 1,805
Default

Quote:
On 2002-04-17 06:35, abz2max wrote:
I need to create a list of random numbers, that do not repeat each other. The range of numbers i will need will be 1 to n. n = any number>1. how can i create a macro to do this.
abz2max,
That issue was discussed in the past for several times.
look for example at:
http://www.mrexcel.com/board/viewtop...c=4990&forum=2
Eli
eliW is offline   Reply With Quote
Old Apr 17th, 2002, 10:15 AM   #3
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

God, I really don't want to turn this into a huge discussion like those other two, but this has been nagging at me:

Dave H suggested the code for picking lottery numbers:


Function RandLotto(Bottom As Integer, Top As Integer, Amount As Integer)
Dim iNum As String
Dim strNum As String
Dim i As Integer

Application.Volatile
iNum = Int((Top - Bottom + 1) * Rnd + Bottom)

For i = 1 To Amount
strNum = Trim(strNum & " " & iNum)
Do Until InStr(1, strNum, iNum) = 0
iNum = Int((Top - Bottom + 1) * Rnd + Bottom)
Loop
Next i

RandLotto= strNum

End Function


I thought about this one day and realised that there is a flaw in this technique. This technique is biased against single digit numbers. That is to say, if "49" came up as the first pick in this code, the numbers "4" and "9" could not be individually picked as a single digit number. This is due to the "instr" check that Dave uses.

Sorry if this turns into a huge discussion again, but this seemed like a good opportunity to address this without revisiting the old threads.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 17th, 2002, 10:46 AM   #4
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi Mark,

Excellent!! Nice catch. Will return unique numbers but is biased.

Thanks for the post.

Bye,
Jay

P.S. FYI, the following post is, in my opinion, the most amazing of this entire Excel/RNG topic.

I have specified the link to Harlan Grove's post, but for those interested, please read the whole thread, including Harlan's discussion with Tushar Mehta afterward.

http://groups.google.com/groups?hl=e...att.net&rnum=5
Jay Petrulis is offline   Reply With Quote
Old Apr 17th, 2002, 11:01 AM   #5
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Good thread. Interesting to me since I have a vague grasp of bias and such statistical "nonsense". (I'm an engineer, statistics have to be treated carefully in my field)

The one thing that interests me in Harlan's code is that ahe doesn't use "Randomize" to initialise VB's RNG. Now, I ran into a problem with this myself. I made a little High/Low guessing game in Excel. (kind of like "Play Your Cards Right" and whatever the name of the US version was might have been "Jokers Wild") Anyway, without initialising the RNG I found that if I restarted to play my game again, I got the same "random" numbers each time.

I think using "Randomize" changes the seed. Again, I don't want to turn this into a hee-yuge discussion, but I know that it's going to head that way.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 17th, 2002, 11:29 AM   #6
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

I just posted to .worksheetfunctions asking this. I should get a few responses soon, and Harlan will respond if he sees the post.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old Apr 17th, 2002, 11:49 AM   #7
Jack in the UK
Board Regular
 
Join Date: Feb 2002
Posts: 3,065
Default

why not find at random.. not produce random?


__________________
Free Excel based Web Toolbar available here.

Jack in the UK
J & R Excel Solutions
"making Excel work for you"
Jack in the UK is offline   Reply With Quote
Old Apr 17th, 2002, 02:11 PM   #8
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

Here is Harlan's response:

Quote:
Jay Petrulis wrote...
....
>I have one question, for Harlan in particular, about his
>code. Could it be improved (made "more random") if
>
>Randomize
>
>is used to initialize the RNG and was included before the
>VBA Rnd function is invoked, so that the "seed" is
>reshuffled? Was/is it unnecessary in this instance?
....

It never hurts. Just because I didn't include it doesn't mean I had a reason
not to include it other than simply forgetting to do so.

Note: all Rnd calls in all VBA procedures use the same pseudorandom number
generator, so it's only necessary to call Randomize once in any Excel
session (though I guess it might be necessary to call it in each additional
Excel application instance if using multiple Excel sessions - dunno whether
the state variables are stored in shared DLL memory space or in each
application instance's own memory space). I call Randomize in Worksheet_Open
in my Personal.xls file, so I never think of adding it elsewhere.
Didn't know that one session thing. Calling Randomize when Excel is opened is pretty cool. Adding Randomize to his code would make his routine even better, for those who haven't already initialized the RNG with it.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 12:07 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes