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 Feb 16th, 2009, 04:45 PM   #1
mgirvin
 
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
Default Random Numbers Not Divisible By 3

Dear Most Amazing Excel Team,

With a formula, how could I generate random numbers between 1 and 100 that are not divisible by 3?

I made this part of a formula that generates the numbers not divisible by 3:

MOD(ROW(INDIRECT("1:100")),3)<>0

But I am unsure how to use it to help to generate random numbers between 1 and 100 not divisible by 3.

Any ideas?
__________________
Sincerely, Mike Gel Girvin
mgirvin is offline   Reply With Quote
Old Feb 16th, 2009, 05:00 PM   #2
Jon von der Heyden
 
Jon von der Heyden's Avatar
 
Join Date: Sep 2004
Location: Hampshire, England
Posts: 4,018
Default Re: Random Numbers Not Divisible By 3

Hello Mike

Not very elegant but it's all I could think of:
PHP Code:
=INDEX({1;2;4;5;7;8;10;11;13;14;16;17;19;20;22;23;25;26;28;29;31;32;34;35;37;38;40;41;43;44;46;47;49;50;52;53;55;56;58;59;61;62;64;65;67;68;70;71;73;74;76;77;79;80;82;83;85;86;88;89;91;92;94;95;97;98;100},RANDBETWEEN(1,67)) 
__________________
Regards


Jon (XP Pro v.2002, XL2003)
Jon von der Heyden is offline   Reply With Quote
Old Feb 16th, 2009, 05:28 PM   #3
barry houdini
MrExcel MVP
 
Join Date: Mar 2005
Location: England
Posts: 14,292
Default Re: Random Numbers Not Divisible By 3

You could use this formula

=SMALL(IF(MOD(ROW(INDIRECT("1:100")),3)<>0,ROW(INDIRECT("1:100"))),INT(RAND()*67+1))

confirmed with CTRL+SHIFT+ENTER
barry houdini is offline   Reply With Quote
Old Feb 16th, 2009, 05:49 PM   #4
LucB
 
Join Date: Apr 2005
Posts: 52
Default Re: Random Numbers Not Divisible By 3

Try this Macro.

Option Explicit

Sub DivThreeOut()
Dim n As Long, i As Long

For n = 1 To 10
Randomize
i = Int(99 * Rnd) + 1
If i Mod 3 <> 0 Then
Range("A" & n) = i
End If
Next n
End Sub
LucB is offline   Reply With Quote
Old Feb 16th, 2009, 06:45 PM   #5
mgirvin
 
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
Default Re: Random Numbers Not Divisible By 3

Dear Amazing Smartest Excel Team In The World,

I love getting three different answers to the same problem because it helps me to see answers from different points of views; which in turn helps me to know more about Excel!

Thanks Jon von der Heyden, the formula may be less elegant than others, but understanding it is straight forward! And since I have to explain this to the person who asked me, that person may prefer this to a SMALL / IF formula.

barry houdini, I was thinking along these lines with the IF(MOD(ROW(INDIRECT("1:100")),3)<>0,ROW(INDIRECT("1:100"))),, But the SMALL and INT / RAND you added was the conceptual kick I needed!! EXCELlent formula!

LucB, Thanks for the VBA code. It does exactly what I want (and helps formula-geeks-know-little-about-VBA guys like me to see the broader Excel picture)!

You all are great!
__________________
Sincerely, Mike Gel Girvin
mgirvin is offline   Reply With Quote
Old Feb 16th, 2009, 06:56 PM   #6
mikerickson
MrExcel MVP
 
mikerickson's Avatar
 
Join Date: Jan 2007
Location: Davis CA
Posts: 8,453
Default Re: Random Numbers Not Divisible By 3

This will return a random number between 1 and 100 that is not divisible by 3
=INT(RAND()*33)*3+ROUNDUP(RAND()*2,0)
mikerickson is offline   Reply With Quote
Old Feb 16th, 2009, 10:24 PM   #7
mgirvin
 
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
Default Re: Random Numbers Not Divisible By 3

Dear mikerickson,

Awesome! That certainly is the fastest calculating one. Let me ask you these questions:

Is 96 the greatest value that this can ever get:

INT(RAND()*33)*3

Is 2 the greatest value that this can ever get:

ROUNDUP(RAND()*2,0)

Is it possible to get a 100 with this formula? Or is 98 the biggest value?

I could not get 100 with this formula.

If it is true that 98 is the biggest, is there a way to tweak it. Because this would be the fastest.
__________________
Sincerely, Mike Gel Girvin
mgirvin is offline   Reply With Quote
Old Feb 16th, 2009, 10:44 PM   #8
barry houdini
MrExcel MVP
 
Join Date: Mar 2005
Location: England
Posts: 14,292
Default Re: Random Numbers Not Divisible By 3

Hello Mike.....and Mike....

Not very likely but, as RAND() can return zero it's possible that

=ROUNDUP(RAND()*2,0)

could also return zero, in which case you'd get a number divisible by 3, so I'd suggest revising the second part of Mike's formula along the same lines as the first, i.e.

=INT(RAND()*33)*3+INT(RAND()*2+1)

Of course that still can't return 100 so, perhaps not very elegant, but you could add an IF function like this

=IF(RAND()>1/67,INT(RAND()*33)*3+INT(RAND()*2+1),100)
barry houdini is offline   Reply With Quote
Old Feb 16th, 2009, 11:06 PM   #9
mikerickson
MrExcel MVP
 
mikerickson's Avatar
 
Join Date: Jan 2007
Location: Davis CA
Posts: 8,453
Default Re: Random Numbers Not Divisible By 3

The highest number that is returned by a formula similar to mine would =2 mod 3, so 98 is the highest possible result of the given forumula.
Change the 33 to 34 and the highest becomes 101.
mikerickson is offline   Reply With Quote
Old Feb 17th, 2009, 05:37 AM   #10
mgirvin
 
Join Date: Dec 2005
Location: Seattle, WA
Posts: 321
Default Re: Random Numbers Not Divisible By 3

Thank you houdini and mikerickson for the additional comments!
__________________
Sincerely, Mike Gel Girvin
mgirvin 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 +1. The time now is 07:43 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 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