Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default tricky rand situation

i am using the rand function to generate a number which is to be given
to each customer who fills in my form. Their number and details will be
placed in a look up table. How could i prevent the same number coming
up out of the 10,000,000 possible outcomes when i multiply the rand
function by 10,000,000.


---
Message posted from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default tricky rand situation

Noctos,

Check the initial result against your record table: if you find the
value, generate another random number, and continue until you don't
find it in the table. If you use the MATCH function with the last
parameter = FALSE (to find exact matches), it will return an error
when you have a unique number.

HTH,
Bernie
MS Excel MVP


"Noctos " wrote in message
...
i am using the rand function to generate a number which is to be

given
to each customer who fills in my form. Their number and details will

be
placed in a look up table. How could i prevent the same number

coming
up out of the 10,000,000 possible outcomes when i multiply the rand
function by 10,000,000.


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default tricky rand situation

yea the thing is the user will be doing this by thereselves so i won't
be there to keep on checking if the rand number to do. I think the best
way is to use vba. The rand number will be generated and this number
can be compared to the table using the match value if a value is found
then the rand number could be refreshed like a loop until a unique
value is found and this value is then displayed. Does anyone know how
to translate this into vba code


---
Message posted from http://www.ExcelForum.com/

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default tricky rand situation

Noctos,

The sub below will store the unique number appearing in cell A1 in
column B of the same sheet.

HTH,
Bernie
MS Excel MVP

Sub TryNow()
Dim boolMatch As Boolean
boolMatch = True

While boolMatch
Range("A1").Value = "'" & Format(Rnd() * 10000000, "0000000")
If Application.WorksheetFunction.CountIf(Range("B:B") ,
Range("A1").Value) = 0 Then
boolMatch = False
Range("B65536").End(xlUp)(2).Value = "'" & Range("A1").Text
End If
Wend

End Sub


"Noctos " wrote in message
...
yea the thing is the user will be doing this by thereselves so i

won't
be there to keep on checking if the rand number to do. I think the

best
way is to use vba. The rand number will be generated and this number
can be compared to the table using the match value if a value is

found
then the rand number could be refreshed like a loop until a unique
value is found and this value is then displayed. Does anyone know

how
to translate this into vba code


---
Message posted from http://www.ExcelForum.com/



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default tricky rand situation

Why generate a random number. Sounds like all you need is a unique number

? Format(now,"yymmddhhmmss")
040102150108



--
Regards,
Tom Ogilvy


Noctos wrote in message
...
yea the thing is the user will be doing this by thereselves so i won't
be there to keep on checking if the rand number to do. I think the best
way is to use vba. The rand number will be generated and this number
can be compared to the table using the match value if a value is found
then the rand number could be refreshed like a loop until a unique
value is found and this value is then displayed. Does anyone know how
to translate this into vba code


---
Message posted from http://www.ExcelForum.com/





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default tricky rand situation

not entirely sure how to use your unique number tom but bernie vb is
saying there is an error in these two lines

If Application.WorksheetFunction.CountIf(Range("B:B") ,
Range("A1").Value) = 0 Then


---
Message posted from http://www.ExcelForum.com/

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default tricky rand situation

You are a victim of email wordwrap. It should all be one line, or put in a
line continuation character (space underscore):

If Application.WorksheetFunction.CountIf(Range("B:B") , _
Range("A1").Value) = 0 Then

--
Regards,
Tom Ogilvy




Noctos wrote in message
...
not entirely sure how to use your unique number tom but bernie vb is
saying there is an error in these two lines

If Application.WorksheetFunction.CountIf(Range("B:B") ,
Range("A1").Value) = 0 Then


---
Message posted from http://www.ExcelForum.com/



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 733
Default tricky rand situation

"Tom Ogilvy" wrote...
Why generate a random number. Sounds like all you need is a unique number

? Format(now,"yymmddhhmmss")
040102150108


While it may be rare, this technique can cause problems if multiple users
could be using different copies of the workbook (or its forms)
simultaneously. The problem would be similar to generating unique invoice
numbers when there are multiple simultaneous users. If that were so, then
some form or centralized generation of unique numbers is unavoidable.


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default tricky rand situation

Assuming a single user per customer
Easily solved by appending some unique identification information for the
customer.

All schemes would be made more robust by using a central issuing authority,
but that didn't appear to be the intent of the OP.

--
Regards,
Tom Ogilvy

Harlan Grove wrote in message
...
"Tom Ogilvy" wrote...
Why generate a random number. Sounds like all you need is a unique

number

? Format(now,"yymmddhhmmss")
040102150108


While it may be rare, this technique can cause problems if multiple users
could be using different copies of the workbook (or its forms)
simultaneously. The problem would be similar to generating unique invoice
numbers when there are multiple simultaneous users. If that were so, then
some form or centralized generation of unique numbers is unavoidable.




  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 733
Default tricky rand situation

"Tom Ogilvy" wrote...
....
All schemes would be made more robust by using a central issuing authority,
but that didn't appear to be the intent of the OP.

....

Perhaps. In my own experience, unique ID generation is highly susceptible to
Murphy's Law.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What Should I do in this situation Dave[_11_] New Users to Excel 5 August 20th 09 06:07 PM
how can i set up rand or rand between to give only -1 or 1? Uesiet Excel Worksheet Functions 8 October 27th 08 02:28 PM
"Tricky Situation" - Validation Formula Teddy-B Excel Discussion (Misc queries) 4 November 24th 07 07:43 PM
Nested IF in this situation Cossminnn Excel Worksheet Functions 6 May 20th 06 11:45 AM
Offset Situation Jennifer Excel Discussion (Misc queries) 2 June 30th 05 11:39 AM


All times are GMT +1. The time now is 01:43 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"