Re: [android-developers] How to generate unique Random Numbers between 1 to 9
That's not true, it can still be random: just over a different state
space. (In particular [0-9]^n \ {alpha^n | any character is repeated
in alpha})
OP: you could pull out a statistics book and read about random events,
how to generate them using coins, etc... But the easiest way is to
generate a random number, add it to the list if it's not already in
the list, etc... The uniformity argument works based on the invariant
that at each stage the next number you choose to "fix" the rest of
your choices is random, hence the distribution remains random.
# let assemble_random_nums len =
let rand () = (Random.int 9) + 1 in
let rec h l = function
| 0 -> l
| i -> let r = rand () in if (List.mem r l) then (h l i) else (h
(r::l) (i-1))
in
h [] len;;
val assemble_random_nums : int -> int list = <fun>
# assemble_random_nums 4;;
- : int list = [1; 8; 3; 9]
# assemble_random_nums 4;;
- : int list = [2; 9; 5; 4]
# assemble_random_nums 4;;
Kris
On Wed, Jun 19, 2013 at 7:03 AM, Michael Banzon <michael@banzon.dk> wrote:
> First: This isn't really a question related to Android development.
>
> Second: Random unique numbers between 1 and 9 (including both
> extremes) is a set of 9 numbers (I really hope that we are talking
> about integers and not real/floating point numbers).
>
> Third: If you request a unique sequence of numbers 1 to 9 it can't be
> random as it would include a check for uniqueness that would remove
> some degree of randomness.
>
> On Wed, Jun 19, 2013 at 11:56 AM, vamshi ch <vamshi0408@gmail.com> wrote:
>> Hi All,
>>
>> In my application i want to generate random numbers between 1 to 9 and
>> that is unique. once i get the random number that should not be repeated for
>> any one, any where and for me also. it's similar to OTP(one time password)
>> but some time OTP will be repeated and that is alphanumerical but in my case
>> that should not be happen and i need numbers only.
>>
>> Please can you tell me what is the approach to achieve it. Please help out
>> of this..
>>
>>
>> Please share the code if you have any regarding this..
>>
>> --
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to android-developers@googlegroups.com
>> To unsubscribe from this group, send email to
>> android-developers+unsubscribe@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Android Developers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to android-developers+unsubscribe@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
>
>
> --
> Michael Banzon
> http://michaelbanzon.com/
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home