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 Jan 14th, 2002, 05:14 PM   #1
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

OK, to go back to the original post. Here is a bit of VBA that will give a random order for the range 1 to 233. Each number is used only once. I 100% guarantee that there can be no duplicates because I used an old ZX Spectrum card shuffling technique of using an array to hold which numbers have and have not been used. It took about 1 second for the complete code to run and put data in column A. This is a AMD 233 MHz machine I'm using.


Public Sub main()
Dim iRandArray() As Integer
Dim i As Integer
RandomOrder 233, iRandArray()

For i = 1 To UBound(iRandArray)
Sheets("Sheet1").Range("A1").Offset(i, 0).Value = iRandArray(i)
Next

End Sub
Private Sub RandomOrder(ByVal iNumRange As Integer, ByRef iRandArray() As Integer)

Dim iNumArray() As Integer
Dim iRandValue As Integer

ReDim iNumArray(1 To iNumRange, 1 To 2)
ReDim iRandArray(1 To iNumRange)

For i = 1 To iNumRange
iNumArray(i, 1) = i
iNumArray(i, 2) = 1
Next


For i = 1 To iNumRange
Randomize
iRandValue = Int((iNumRange * Rnd) + 1)

If iNumArray(iRandValue, 2) = 0 Then
i = i - 1
Else
iRandArray(i) = iRandValue
iNumArray(iRandValue, 2) = 0
End If
Next

End Sub


I think that I agree with Jack about Dave's code possibly giving repeats, but he it seems that he reduces the likelyhood of this with the way he's coded it.

HTH
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 12th, 2002, 11:23 AM   #2
clevow
New Member
 
Join Date: Apr 2002
Posts: 4
Default

Does anyone know of a way to force a list of random numbers with no two numbers the same? I need a list 1-233 with each number being used exactly once.

Any help would be appreciated. Thanks!

-Carrie
clevow is offline   Reply With Quote
Old Apr 12th, 2002, 11:31 AM   #3
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-04-12 10:23, clevow wrote:
Does anyone know of a way to force a list of random numbers with no two numbers the same? I need a list 1-233 with each number being used exactly once.

Any help would be appreciated. Thanks!

-Carrie
Hi Carrie,

At the end of the following post (page 3), both Dave Hawley and I posted code to do exactly what you want.

You'll have to change the code to suit your needs (233 in samplem 233 selected), but you won't get duplicates.

Found at this link:
http://www.mrexcel.com/board/viewtop...orum=2&start=0

HTH,
Jay
Jay Petrulis is offline   Reply With Quote
Old Apr 12th, 2002, 11:57 AM   #4
clevow
New Member
 
Join Date: Apr 2002
Posts: 4
Default

I was hoping for a formula, but I guess that's not possible, judging by the thread you referred me to!

I think this'll work. Thanks!
clevow is offline   Reply With Quote
Old Apr 12th, 2002, 11:59 AM   #5
Jack in the UK
Board Regular
 
Join Date: Feb 2002
Posts: 3,065
Default

Hi Jay

These posts clearly satae this rand function FAILS, not 100%, i have VBA thats truly 100% lottery no choose, Chris D i sent it to and it NEVER fails,,,,,, this can easy be converted to 223 and select as many diff nos as reqd.

OK can this be one cell one formula if nested return the 6 numbers of 233 numbers...

YES i truly beleive the girls i love so much "excel" can and wil do this its a case of how... so far we are close SOON we will post if hope and get the reqd answer RAND numbers to any situation,,, ALL One cell that means looking at its self...

let me know ill post code.. and amend... formual not so easy


__________________
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 12th, 2002, 01:17 PM   #6
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-04-12 10:59, Jack in the UK wrote:
Hi Jay

These posts clearly satae this rand function FAILS, not 100%, i have VBA thats truly 100% lottery no choose, Chris D i sent it to and it NEVER fails,,,,,, this can easy be converted to 223 and select as many diff nos as reqd.

OK can this be one cell one formula if nested return the 6 numbers of 233 numbers...

YES i truly beleive the girls i love so much "excel" can and wil do this its a case of how... so far we are close SOON we will post if hope and get the reqd answer RAND numbers to any situation,,, ALL One cell that means looking at its self...

let me know ill post code.. and amend... formual not so easy


Hi Jack,

Tom's code will not repeat. Neither does Dave's UDF. Both of these overcome the problems the OP from that thread was having.

Please post your code, so that we can see another way. But, I think the OP in this thread will have success with either of the two referenced posts.

Thanks,
Jay

Jay Petrulis is offline   Reply With Quote
Old Apr 12th, 2002, 01:22 PM   #7
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-04-12 10:57, clevow wrote:
I was hoping for a formula, but I guess that's not possible, judging by the thread you referred me to!

I think this'll work. Thanks!
Dave Hawley's RandLotto is a function you can call from a worksheet cell.

I am unsure what you need. If you need a sequential list in random order, Tom Ogilvy's code will work, too.
Jay Petrulis is offline   Reply With Quote
Old Apr 12th, 2002, 01:29 PM   #8
Jack in the UK
Board Regular
 
Join Date: Feb 2002
Posts: 3,065
Default

Hi Jay

Dave Hawley is a very close friend as far ways as weare in the world and i would agree withDave he say white and i dsay black ill still agree,

this code will fail, Daves notr convinced...

Aladins a great pal also and again a will agree and except his call regardless, these guys are never less than perfect, but the post will fail..

the feed do not say they will be 100% randon.

I beleive Rand is not random, and thisis as i have proved to Chris D yesterday i can predict the future rand answer OK will take time but thats not random as i see it, im not saying the answers not OK im am saying RANDON ie rand is really random..

Chris will but in,, and tell his side

the problem in RAND as we use it and as these posts do is its contained, so is workable and predictable,....

Rand 1-10 i can guess ok i only guess correct 2 in ten is that rand... but guess rand in enfinity as Buz Ligtyear shouts and go beyond then thats random... and control is not random.... bit random between two goal posts.

Chris and i are working TRUE randon...

fut find in selection id random as hjow can you apply maths to find.. you copuld say porberbility but that not the same question as rand..

is random randon if controlled.. i say not..

__________________
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 12th, 2002, 01:44 PM   #9
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

the poster in this thread just wants a formula, which is entirely achievable using more than one cell or UDFs or named rnages and named formulae, so no big dramas there.

The challenge in the Easter posting was either a) a single formula with no precedents, just a totally self-encapsulated formula or b) a single line of VBA

(no referencing cells, no named formulae nothing).... this is still open and unresolved)

in my opinion also, it should also stand up to mathematical scrutiny in terms of true randomness and the probablitiy of duplicates - but I knwo this board has a very deep wealth of people who can comment on this if the formula ever surfaces
Chris Davison is offline   Reply With Quote
Old Apr 12th, 2002, 01:55 PM   #10
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

Quote:
On 2002-04-12 10:23, clevow wrote:
Does anyone know of a way to force a list of random numbers with no two numbers the same? I need a list 1-233 with each number being used exactly once.

Any help would be appreciated. Thanks!

-Carrie
in cell A1 try : =RAND()*100,000,000,000

and copy down to cell A233

in cell B1 try : =RANK(A1,$A$1:$A$233,1)

and copy down to cell B233

in column B you now have a ranodmly-ordered list of the numbers from 1 to 233 with the probablitiy of a repeat of around a hundred thousand million to one (Celtic winning the double next year, again)


__________________
:: Pharma Z - Family drugstore ::
Chris Davison 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 09:15 AM.


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