[android-developers] Re: Unique Random numbers
Assuming that you have a rand() function (that might repeat) and that
your value range is [0, maxValue - 1]: Create an array of size
maxValue and initialize everything to its index:
for (int i = 0; i < maxValue; ++i) {
arr[i] = i;
}
Now do this:
for (int i = 0; i < maxValue - 1; ++i) {
tmp = rand() % (maxValue - i);
swap(arr[i], arr[maxValue - tmp]);
}
This shuffles the array in a random way, and walking through the array
gives you values that can't repeat.
On Mar 29, 11:04 am, Andrew Gregory <andrew.greg...@gmail.com> wrote:
> On 25 March 2011 20:45, Hassy <hasith....@gmail.com> wrote:
>
>
>
> > I want to display unique random numbers when button clicked.
> > if a number appear once, that number should not appear again when
> > button clicked.
>
> http://en.wikipedia.org/wiki/Linear_feedback_shift_register
>
> Looks random (and has been used as a random number generator at times), but
> doesn't repeat until the entire sequence has been used (maximal length).
>
> --
> Andrew
--
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
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home