RANDBETWEEN with ascending order

khardu

Board Regular
Joined
Jan 18, 2012
Messages
68
Hello dear excel users,


how can I use RANDBETWEEN to list the result in an ascending order.







Thanking you.




Regards,




KHARDU
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Care to provide a tiny example which shows your intent?

Thank you Aladin Akyurek for you reply.

Here is the case. RANDBETWEEN (1000,3099) this will generate numbers from 1000 to 3099. What I want is to put those numbers in an ascending order with the lowes number at the top and the highest at bottom. Iam applying RANDBETWEEN on less than 30 cells, i.e., I need 30 numbers from the range 1000-3099 in ascending order. Hope this make sense.


Thanking you.





Regards,





KHARDU
 
Upvote 0
Just posting to see the better solution form Aladin.
in A2 =RANDBETWEEN(1000,3099)
copied till Row 30
in B2 =SUM(1*(A2>$A$2:$A$30))+1+IF(ROW(A2)-ROW($A$2)=0,0,SUM(1*(A2=OFFSET($A$2,0,0,INDEX(ROW(A2)-ROW($A$2)+1,1)-1,1))))
Ctrl + Shift + Enter, not just enter.
Copied till B30
in C2 till C30 =RANK(B2,$B$2:$B$30,1)
In D2 till D30 type the number 1 to 30 then in E2 till E30 type =INDEX($A$2:$A$30,MATCH(D2,$C$2:$C$30,0)) CSE
that would give (with so many helper columns):
<style> <!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";} @page {margin:1.0in .75in 1.0in .75in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-page-orientation:landscape;} td {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:black; font-size:12.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:Calibri, sans-serif; mso-font-charset:0; mso-number-format:General; text-align:general; vertical-align:bottom; border:none; mso-background-source:auto; mso-pattern:auto; mso-protection:locked visible; white-space:nowrap; mso-rotate:0;} .xl63 {mso-number-format:"0\.0";} --> </style> [TABLE="width: 325"]
<tbody>[TR]
[TD="class: xl63, width: 65, align: right"]1784.0[/TD]
[TD="width: 65, align: right"]14[/TD]
[TD="width: 65, align: right"]14[/TD]
[TD="width: 65, align: right"]1[/TD]
[TD="width: 65, align: right"]1008[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1523.0[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1028[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1384.0[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]1074[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2583.0[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]1169[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2911.0[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]1313[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2800.0[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]1345[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1313.0[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]1384[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1008.0[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]1444[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2140.0[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]1455[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2846.0[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]1523[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1169.0[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]1557[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1444.0[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]1596[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1672.0[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]1672[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1596.0[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]14[/TD]
[TD="align: right"]1784[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2832.0[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"]1953[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2709.0[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]2070[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2977.0[/TD]
[TD="align: right"]29[/TD]
[TD="align: right"]29[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]2140[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1455.0[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]2335[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2845.0[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"]2391[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1953.0[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]2583[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2869.0[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"]2709[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1028.0[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"]2733[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2733.0[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"]2800[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1074.0[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"]2832[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2335.0[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]2845[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1557.0[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"]2846[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2391.0[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"]2869[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2070.0[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"]2911[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1345.0[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]29[/TD]
[TD="align: right"]2977[/TD]
[/TR]
</tbody>[/TABLE]

You can hide Columns B to D.
 
Upvote 0
actually column C (=RANK(B2,$B$2:$B$30,1)) is useless... :eeek:
well that leaves you with B, C and the sorted range in D...
 
Last edited:
Upvote 0
Just posting to see the better solution form Aladin.
in A2 =RANDBETWEEN(1000,3099)
copied till Row 30
in B2 =SUM(1*(A2>$A$2:$A$30))+1+IF(ROW(A2)-ROW($A$2)=0,0,SUM(1*(A2=OFFSET($A$2,0,0,INDEX(ROW(A2)-ROW($A$2)+1,1)-1,1))))
Ctrl + Shift + Enter, not just enter.
Copied till B30
in C2 till C30 =RANK(B2,$B$2:$B$30,1)
In D2 till D30 type the number 1 to 30 then in E2 till E30 type =INDEX($A$2:$A$30,MATCH(D2,$C$2:$C$30,0)) CSE
that would give (with so many helper columns):
<style> <!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";} @page {margin:1.0in .75in 1.0in .75in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-page-orientation:landscape;} td {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:black; font-size:12.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:Calibri, sans-serif; mso-font-charset:0; mso-number-format:General; text-align:general; vertical-align:bottom; border:none; mso-background-source:auto; mso-pattern:auto; mso-protection:locked visible; white-space:nowrap; mso-rotate:0;} .xl63 {mso-number-format:"0\.0";} --> </style> [TABLE="width: 325"]
<tbody>[TR]
[TD="class: xl63, width: 65, align: right"]1784.0[/TD]
[TD="width: 65, align: right"]14[/TD]
[TD="width: 65, align: right"]14[/TD]
[TD="width: 65, align: right"]1[/TD]
[TD="width: 65, align: right"]1008[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1523.0[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1028[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1384.0[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]1074[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2583.0[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]1169[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2911.0[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]1313[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2800.0[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]1345[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1313.0[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]1384[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1008.0[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]1444[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2140.0[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]1455[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2846.0[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]1523[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1169.0[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]1557[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1444.0[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]1596[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1672.0[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]1672[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1596.0[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]14[/TD]
[TD="align: right"]1784[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2832.0[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"]1953[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2709.0[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]2070[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2977.0[/TD]
[TD="align: right"]29[/TD]
[TD="align: right"]29[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]2140[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1455.0[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]2335[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2845.0[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"]2391[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1953.0[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]2583[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2869.0[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"]2709[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1028.0[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"]2733[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2733.0[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"]2800[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1074.0[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"]2832[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2335.0[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]2845[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1557.0[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"]2846[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2391.0[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"]2869[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]2070.0[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"]2911[/TD]
[/TR]
[TR]
[TD="class: xl63, align: right"]1345.0[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]29[/TD]
[TD="align: right"]2977[/TD]
[/TR]
</tbody>[/TABLE]

You can hide Columns B to D.

actually column C (=RANK(B2,$B$2:$B$30,1)) is useless... :eeek:
well that leaves you with B, C and the sorted range in D...

I don't understand why the ascending order is needed. And, not sure if the following would be usable...

A2, copied down:

=IF(ROWS($A$2:A2)<=30,RANDBETWEEN(1000,3099),"")

C2, copied down:

=IF(A2="","",SMALL($A$2:$A$31,ROWS($C$2:C2)))
 
Upvote 0
Thnks Aladin, definitely the better answer.
May I ask you why ROWS ($C$2:C2) works so well as k?
the small would get the lowest value, we could have used large for descending order, then Rows would be used for the position of the smallest value in the array right?
 
Upvote 0
Thnks Aladin, definitely the better answer.
May I ask you why ROWS ($C$2:C2) works so well as k?
the small would get the lowest value, we could have used large for descending order, then Rows would be used for the position of the smallest value in the array right?

ROWS($C$2:C2) returns the number of rows in $C$2:C2. In C2 this would return 1, in C3 2, etc. in increasing order. The function operates pretty fast and is non-volatile. For that reson it can be used as ever increasing k in SMALL and LARGE as well as in other situations where such a counter is needed.
 
Upvote 0
Thank you, yes it indeed makes sense. Cool alternative.
 
Upvote 0
Thank you, yes it indeed makes sense. Cool alternative.
It may be easier to understand if you see how it works.

ROWS(C$2:C2) returns the count of rows in the referenced range. C2:C2 is 1 row so ROWS(C$2:C2) = 1. When you copy the formula down a column then the range increments like this:

ROWS(C$2:C2) = 1
ROWS(C$2:C3) = 2
ROWS(C$2:C4) = 3
ROWS(C$2:C5) = 4
ROWS(C$2:C6) = 5
etc
etc
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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