checking each cell in an array whether it satisfies a condition

vizakenjack

New Member
Joined
Oct 8, 2014
Messages
8
So, let's say I need to have 24 numbers randomized.
But in a particular way! They all should be chaotically randomized according to these rules:
half of them (12 cells) should be randomized between 10-60
the rest (also 12 cells) between 80-90

so there's a gap between of 61-79 values, they are to be excluded
but(!) you want to have 12 cells that would lie between 10-60, and the rest between 80-90
And it should be done with a single formula, that is, you can't have a formula
Code:
=Randbetween(10,60)
in first 12 cells (which already breaks the rules, because you don't want to have first 12 cells to be randomized between 10-60, but rather the amount of cells containing the values between 10-60, which are spread out randomly in an array), and then
Code:
=Randbetween(80,90)
in the remainder of the cells.
"Search" function returns the position of a character in a text... totally not what I need. I need a formula that would check each cell in an array/selection on whether it satisfies the condition of being more than 60.
Or is there a way to solve it by using simple formulas? Also, randbetween has to be used here, so please don't suggest advices that do not involve using this formula.
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
visakenjack,

I see you have had no response as yet so here is an offering.......

I assume that duplicate values are not an issue since to pick 12 numbers from the range 80 to 90 guarantees at least one duplicate?

It does require using a 'helper column' and I am unsure as to whether you will find that acceptable?

The helper can be any column you care to use and can be outside your normal dat set and hidden if required.

So using a helper range A1:A24.



Excel 2007
ABC
10.70697815956
20.811114941
30.09521711986
40.26826133884
50.7163620949
60.55236745986
70.92263154213
80.54002183988
90.61620949543
100.69147509822
110.0227316283
120.49913609188
130.06624599689
140.44876205189
150.41561656886
160.20145183887
170.71337905520
180.96371366827
190.6090869625
200.83290592748
210.73502527938
220.28203885780
230.89323890813
240.50713612988
Sheet1
Cell Formulas
RangeFormula
A1=RAND()
C1=IF(RANK(A1,$A$1:$A$24,0)>12,RANDBETWEEN(80,90),RANDBETWEEN(10,60))


Given that the RAND values generated in A are to 9 decimal places I have assumed the chances of duplicates as negligible?
If not the case then the RANK function will need to be modified to provide a tiebreak.

Hope that helps directly or indirectly.
 
Last edited:
Upvote 0
Leave A1 blank,
put this in A2 and drag down to A25

=IF(RAND()<((12-COUNTIF($A$1:$A1,">70"))/(25-ROW(A1))), RANDBETWEEN(80,90), RANDBETWEEN(10,60))


25-ROW(A1) is 24,23,22,... then number of cells remaining to be chosen
COUNTIF(A$1:A1, ">70") is the number of preceding cells in the high range (between 80 and 90)

12-COUNTIF(..) is the number of remaining cells that have to be filled from the high range

(12-COUNTIF(...))/(25-ROW(A1)) is the percentage of remaining cells that have to be filled from the high range

IF(RAND()<((12-COUNTIF)/(25-ROW)), RAND(high), RAND(low)) makes a weighted choice between returning from the high range or the low range.
 
Last edited:
Upvote 0
It does require using a 'helper column' and I am unsure as to whether you will find that acceptable?
I'd rather try to have just one column for random numbers. Also, it's fine if some numbers are repeated.
The helper can be any column you care to use and can be outside your normal dat set and hidden if required.

CellFormula
A1=RAND()
C1=IF(RANK(A1,$A$1:$A$24,0)>12,RANDBETWEEN(80,90),RANDBETWEEN(10,60))

<tbody>
</tbody>

<tbody>
</tbody>
Hm, if that
Rich (BB code):
RANK(A1,$A$1:$A$24,0)>12
Checks the cells above whether 12 of those cells happen to have values that lie between [10,60] - then it should surely help. I don't quite get what the "rank" function does, though...


Leave A1 blank,
put this in A2 and drag down to A25

=IF(RAND()<((12-COUNTIF($A$1:$A1,">70"))/(25-ROW(A1))), RANDBETWEEN(80,90), RANDBETWEEN(10,60))


25-ROW(A1) is 24,23,22,... then number of cells remaining to be chosen
COUNTIF(A$1:A1, ">70") is the number of preceding cells in the high range (between 80 and 90)

12-COUNTIF(..) is the number of remaining cells that have to be filled from the high range

(12-COUNTIF(...))/(25-ROW(A1)) is the percentage of remaining cells that have to be filled from the high range

IF(RAND()<((12-COUNTIF)/(25-ROW)), RAND(high), RAND(low)) makes a weighted choice between returning from the high range or the low range.
Well, if I copy it down in column B, this is what happens. More than 12 cells appear to have values between 80-90...
 
Upvote 0
If you adjust the references to column B, it should work. The ROW(A1) is not column specific, but the range argument for COUNTIF has to match the column that the formulas are in.
 
Upvote 0
If you adjust the references to column B, it should work.
That's strange, but it doesn't really work.
Sometimes it will give more values to the range 80-90, sometimes to the range 10-60
sometimes it seems kinda in between.
It's not consistent...
Okay, I see what's wrong.
=COUNTIF($C$1:$C1,">70") returns zero.
My first cell (C1) is the text ("random numbers"), I tried to adjusting the formula to start it from C2, but it gives me an error ("circular reference").
 
Last edited:
Upvote 0
The formula is correct, $C$1:$C1 should be the reference in C2.

$C$1:$C1 are the cells above the formula, which is what should be counted.
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,401
Members
448,893
Latest member
AtariBaby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top