Author |
Message
|
vickas |
Posted: Wed Oct 23, 2013 10:56 pm Post subject: Generating Unique random number(INteger - 6 digits) |
|
|
Centurion
Joined: 18 Aug 2013 Posts: 126
|
I need to generate a unique random number (must be a integer of only 6 digits )
I used RAND() function , but its generating decimal number.
Is there any other esql numeric function to generate unique random numbers ? If not , how to generate a integer(6 digits) using RAND() ? |
|
Back to top |
|
 |
dogorsy |
Posted: Wed Oct 23, 2013 11:19 pm Post subject: |
|
|
Knight
Joined: 13 Mar 2013 Posts: 553 Location: Home Office
|
Quote: |
how to generate a integer(6 digits) using RAND() ? |
What does the documentation say ?... RAND generates a number between 0.0 and 1.0
Quote: |
Is there any other esql numeric function to generate unique random numbers ? |
Have you learned mathematics at school ? in particular MULTIPLICATION ?
RAND() {the correct maths operation here } and multiplier with the correct number of zeros...
Before asking basic questions you need:
1- read the documentation
2- think about the problem you want to solve and try to find a solution yourself. |
|
Back to top |
|
 |
smdavies99 |
Posted: Wed Oct 23, 2013 11:21 pm Post subject: |
|
|
 Jedi Council
Joined: 10 Feb 2003 Posts: 6076 Location: Somewhere over the Rainbow this side of Never-never land.
|
If the random number is between say 0.0001 and 0.99999 then
Multiply it by the number of digits you need so for 6 digits it is 1,000,000
Then cast to integer or use another method to drop the mantissa.
For a bit more randomness get another random number preferably with a different seed and multiply the second part by 1,000 and OR the results.
Not that hard really. _________________ WMQ User since 1999
MQSI/WBI/WMB/'Thingy' User since 2002
Linux user since 1995
Every time you reinvent the wheel the more square it gets (anon). If in doubt think and investigate before you ask silly questions. |
|
Back to top |
|
 |
gs |
Posted: Wed Oct 23, 2013 11:26 pm Post subject: |
|
|
 Master
Joined: 31 May 2007 Posts: 254 Location: Sweden
|
|
Back to top |
|
 |
dogorsy |
Posted: Wed Oct 23, 2013 11:36 pm Post subject: |
|
|
Knight
Joined: 13 Mar 2013 Posts: 553 Location: Home Office
|
Of course, there are other ways of doing it, for example, write your own function, you will need a prime number as a base and a simple algorithm.
Or you can use part of the timestamp, for example milliseconds.
you need to think what is better for you. |
|
Back to top |
|
 |
vickas |
Posted: Thu Oct 24, 2013 12:59 am Post subject: |
|
|
Centurion
Joined: 18 Aug 2013 Posts: 126
|
@ Dogorsy !!
I believe that am a gud math student !!
and i just wonder how kud you ask me to multiple with 10000 to my RAND result,etc ...
I was very particular of getting only 6 random digits.
RAND generated 0.001251258885 for the first time , and the second time it gave me 0.5635853144 , and if this is the case, multiplying with 1,000000 wud not give me the result with 6 digts al the time..
and I know that we can generate a 6 digit random number using current timestamp milisec,micro sec format ! |
|
Back to top |
|
 |
smdavies99 |
Posted: Thu Oct 24, 2013 1:06 am Post subject: |
|
|
 Jedi Council
Joined: 10 Feb 2003 Posts: 6076 Location: Somewhere over the Rainbow this side of Never-never land.
|
Ok
0.001251258885 * 10exp6 = 1251.258885 drop mantissa and cast as char
= '1251'
Overlay onto '000000' gives '001251'
0.5635853144 * 10exp6 = 563585.3144 drop mantissa and cast as char = '563585'
Overlay onto '000000' gives '563585'.
You could always cast the number to a char using a relevant format clause that would include any leading zero's.(my preferred method but requires more investigation and testing to get it right)
Why don't you try a few things for yourself? _________________ WMQ User since 1999
MQSI/WBI/WMB/'Thingy' User since 2002
Linux user since 1995
Every time you reinvent the wheel the more square it gets (anon). If in doubt think and investigate before you ask silly questions. |
|
Back to top |
|
 |
dogorsy |
Posted: Thu Oct 24, 2013 2:07 am Post subject: |
|
|
Knight
Joined: 13 Mar 2013 Posts: 553 Location: Home Office
|
vickas wrote: |
@ Dogorsy !!
I believe that am a gud math student !!
|
maybe you are. Have you got to the times tables yet ? what about multiplication ? |
|
Back to top |
|
 |
joebuckeye |
Posted: Thu Oct 24, 2013 4:38 am Post subject: |
|
|
 Partisan
Joined: 24 Aug 2007 Posts: 365 Location: Columbus, OH
|
And just because you can generate a random 6 digit number doesn't mean it will all be unique either. There is no telling when you will get a previously generated number.
Which is more important? Being random or being unique? |
|
Back to top |
|
 |
dogorsy |
Posted: Thu Oct 24, 2013 5:27 am Post subject: |
|
|
Knight
Joined: 13 Mar 2013 Posts: 553 Location: Home Office
|
joebuckeye wrote: |
And just because you can generate a random 6 digit number doesn't mean it will all be unique either. There is no telling when you will get a previously generated number.
Which is more important? Being random or being unique? |
But the OP is a "gud math student !! " so he should know that random is just that, and could be repeating ... or maybe I am misunderstanding what the OP means by "gud" |
|
Back to top |
|
 |
kimbert |
Posted: Thu Oct 24, 2013 6:45 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
maybe I am misunderstanding what the OP means |
Yes, that's possible. The OP has explained the problem. We have suggested solutions. Let's leave it there. _________________ Before you criticize someone, walk a mile in their shoes. That way you're a mile away, and you have their shoes too. |
|
Back to top |
|
 |
vickas |
Posted: Fri Oct 25, 2013 1:23 am Post subject: |
|
|
Centurion
Joined: 18 Aug 2013 Posts: 126
|
@ smdavies99
This solution works fine when i send a single message(contains multiple records).
RAND() is generating the same number for the second message(contains multiple records) as well. |
|
Back to top |
|
 |
smdavies99 |
Posted: Fri Oct 25, 2013 1:29 am Post subject: |
|
|
 Jedi Council
Joined: 10 Feb 2003 Posts: 6076 Location: Somewhere over the Rainbow this side of Never-never land.
|
vickas wrote: |
@ smdavies99
This solution works fine when i send a single message(contains multiple records).
RAND() is generating the same number for the second message(contains multiple records) as well. |
That's why you need to change the SEED every time.
http://en.wikipedia.org/wiki/Random_seed
Changing it TWICE for every output is IMHO pretty good. Remember that using six digits limits you to 999,999 different numbers.
You may have to call a java function that will allow you to change the seed. _________________ WMQ User since 1999
MQSI/WBI/WMB/'Thingy' User since 2002
Linux user since 1995
Every time you reinvent the wheel the more square it gets (anon). If in doubt think and investigate before you ask silly questions. |
|
Back to top |
|
 |
fjb_saper |
Posted: Fri Oct 25, 2013 4:55 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
And if you really need distinct numbers not so much as random, have you considered taking the last 6 chars of BROKERUUIDASCHAR ?  _________________ MQ & Broker admin |
|
Back to top |
|
 |
Esa |
Posted: Fri Oct 25, 2013 5:06 am Post subject: |
|
|
 Grand Master
Joined: 22 May 2008 Posts: 1387 Location: Finland
|
|
Back to top |
|
 |
|