Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Generate Random Number between 1-50 without repeats

How can I generate Random Numbers from 1050 without repeats

Posted on Oct 2, 2015 3:39 PM

Reply
Question marked as Best reply

Posted on Oct 5, 2015 9:11 AM

Choose Utilities from the FInder's Go menu, open the AppleScript Editor, and run:


set the_numbers to {}

repeat until (count the_numbers) is 10 --change this number as needed; if this is greater than 50, the script won't finish

set the_num to random number from 1 to 50

if the_num is not in the_numbers then set the_numbers to the_numbers & the_num

end repeat

the_numbers


If you'd like to copy and paste the result into Numbers or another spreadsheet, add the following:


set the_string to ""

repeat with this_number in the_numbers

set the_string to the_string & this_number & return

end repeat

set the_string to items 1 thru -2 of the_string as string


(134319)

16 replies
Question marked as Best reply

Oct 5, 2015 9:11 AM in response to Robert E. Mayorga

Choose Utilities from the FInder's Go menu, open the AppleScript Editor, and run:


set the_numbers to {}

repeat until (count the_numbers) is 10 --change this number as needed; if this is greater than 50, the script won't finish

set the_num to random number from 1 to 50

if the_num is not in the_numbers then set the_numbers to the_numbers & the_num

end repeat

the_numbers


If you'd like to copy and paste the result into Numbers or another spreadsheet, add the following:


set the_string to ""

repeat with this_number in the_numbers

set the_string to the_string & this_number & return

end repeat

set the_string to items 1 thru -2 of the_string as string


(134319)

Oct 2, 2015 5:01 PM in response to Robert E. Mayorga

The chance of having a duplicate in 1050 rows of RAND() function is pretty slim. Nonetheless, instead of using RAND() all by itself here is a way that will guarantee all are different:


=ROUND(RAND(),6)+ROW()/10^10


The 6 most significant digits will random. The last 4 digits are unique to the row. You can round the RAND() piece of it to more decimals to minimize the occurrence of duplicates in the most significant digits and divide the ROW() by a larger multiple of ten so the two don't overlap. As far as randomness goes, if there are any repeats in RAND(), the ones in the lower rows (higher row numbers) will be larger than those in upper rows. It probably makes no difference to what you are doing.

Oct 2, 2015 5:21 PM in response to Badunit

=ROUND(RAND(),9)+ROW()÷10^13 will have the 9 most significant digits random and the last 4 unique to the row.


Edit: I noticed I answered what you asked in your post but not what you asked in the title. RAND() provides a number between 0 and less than 1. So multiply by 49 and add 1 to get as close as you can get to 1-50. You have no chance of getting either 1 or 50 but the distribution in between should be okay.

Oct 5, 2015 9:21 AM in response to Robert E. Mayorga

Hello


I presume what you want is a subset of shuffled distinct integers in range [1, 50], which can be obtained from column A of the following table when sorted by random numbers in column B.


User uploaded file



Shuffled (excerpt) A1 shuffled A2 1 A3 2 A4 3 A5 4 B1 rand B2 =RAND() B3 =RAND() B4 =RAND() B5 =RAND()




* Table is built with Numbers v2.



Hope this may help,

H

Oct 3, 2015 4:48 AM in response to Robert E. Mayorga

Robert E. Mayorga wrote:


MY APOLOGIES to the Numbers Community. I goofed; The title is correct ! - My POST is wrong...

My question was "How to Generate Random Number Between 1-50 without Repeat. The (My) post came out wrong. I pressed the Zero key instead of the dash. I am sorry, Bob Mayorga


No problem. There was a suggestion to use RANDBETWEEN but you are likely to have duplicates with it. Hiroto's idea is to have a list of the numbers 1-50 and then mix them up. He did not show a function that does the re-ordering (he suggested sorting by the column of random numbers). RAND will work fine for manual sorting but if you want it to be automatic, the formulas I know of require all the random numbers to be unique. RAND() still has a very very very very very extremely unlikely chance of a repeat (did I say "very" enough times? probably not).


In the screenshot below, the formula in D is the one that does the reordering. It uses "ROW()-1" as the index. Row 2 will find the 1st largest random number from column B and return the number beside it in column C. Row 3 will find the 2nd largest random number, etc. Using ROW() makes the formula able to be filled down vs hardcoding it with a 1, then a 2, then a 3, etc. If you put the formula somewhere else in your table, you will have to adjust that part of it (and other changes may have to be made, like using ranges instead of full column references).


User uploaded file

Oct 3, 2015 2:17 PM in response to Robert E. Mayorga

Hello


OK. Here's another table which yields automatically shuffled integers.


User uploaded file



Shuffled (excerpt) A1 rand A2 =RAND() A3 =RAND() A4 =RAND() A5 =RAND() B1 rank B2 =RANK(A2,A) B3 =RANK(A3,A) B4 =RANK(A4,A) B5 =RANK(A5,A) C1 distinct? C2 =ISERROR(MODE(B)) C3 C4 C5




* C2 is to check if there's no repeated integers in column B, just in case.


Regards,

H

Oct 5, 2015 1:26 PM in response to Robert E. Mayorga

Here's another way:


User uploaded file

D1=COLUMN()−4

select D1, copy

select D1 to the end of row 1, paste


B2=IFERROR(OFFSET($D1, 0, C2), "")

C2=IF($A$1, RANDBETWEEN(0, MAX($1:$1)−(ROW()−2)), RANDBETWEEN(0, MAX($1:$1)−(ROW()−2)))

D2=IFERROR(IF((COLUMN()−4)≥$C2, E1, D1), "")

Select D2, copy

select cells D2 thru the end of row 2, paste


select cells B2 thru the end of row 2

hover the cursor over the bottom edge of the selection and drag the yellow dot down to the bottom of the table


the randomized values without duplicates will be in column C



Here is a dropbox link:

https://www.dropbox.com/s/pvtxcmt5a76wxty/RandomNoDups.numbers?dl=0

Oct 7, 2015 5:32 AM in response to Wayne Contello

Made some refinements to make the random list appear in a separate table:

User uploaded file

Add a new two column Table and call it "Random", then name the big table "Work"

copy the checkbox from the Work table cell A1 to the Random table cell A1



For the table Work:

C2=IF(Random::$A$1, RANDBETWEEN(0, MAX($1:$1)−(ROW()−2)), RANDBETWEEN(0, MAX($1:$1)−(ROW()−2)))

select cell C2, copy

select cells C2 thru the end of column C, paste



For the table Random

B1=Work::B2

select cell B1, copy

select column B, paste

Oct 9, 2015 9:50 AM in response to Robert E. Mayorga

Bob,


No worries. I'm sure it looks ominous.


If you follow the directions carefully you should be able to make the table yourself. Knowing this may be a challenge, I provided a link so you can download the document. Did you try that?


Here is is again:

Here is a dropbox link:

https://www.dropbox.com/s/pvtxcmt5a76wxty/RandomNoDups.numbers?dl=0

Generate Random Number between 1-50 without repeats

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.