If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: generating unique random numbers

Generate random numbers and save them in an array. But don't save them if
they are already present in the array. Continue until the array is full.

PC2

"Dave" <makinis3@hotmail.com> wrote in message news:3cd9273c$1@10.1.10.29...
>
> I was wondering how do i go about generating unique random numbers?
>
> for example:
>
> i want to generate 5 numbers between 1-10. i want no repeating numbers to
> occur.
>
>
> thanks to anyone who can help.

Re: generating unique random numbers

"Dave" <makinis3@hotmail.com> wrote in message news:3cd9273c$1@10.1.10.29...
>
> I was wondering how do i go about generating unique random numbers?
>
> for example:
>
> i want to generate 5 numbers between 1-10. i want no repeating numbers to
> occur.
>
>
> thanks to anyone who can help.

1) Create an array containing the numbers 1-10.
2) Randomly shuffle the array.
3) Take the first 5 numbers of the array.

Re: generating unique random numbers

Dave,

I recently wrote an application that took a very large text file (used for
data mining) and split the original file into two new text files. I then
filled the two new text files with random lines from the original, with no
replacement. I'll give you some quick psudeocode, if you need the complete
algorithm you can write me at crutcher@tamu.edu.

// I don't remember the exact semantics of this statement, look it up
// in the jdk docs

randomNumberSource.removeElementAt(tempInt);
range--;
}

This code creates a Vector (could be a list or whatever) filled with all
of the possible values for the int[]. For each number to add to the int[]
the total size of the source vector (represented by the range variable) is
multiplied by a random number from 0 to 1. This gives an random index to
the source vector. After the selected index is created the int at that spot
is added to the target int[], the selected int is removed from the source,
and the variable representing the range is reduced so that the next random
index generated using the range will not go out of bounds when referencing
the Vector. Although after the first iteration through the second for loop
the index will not = the int taken, it won't matter. You'll still get 5
random ints, with no duplicates. Sorry for the long response, this issue
has some suprising complexities (if you don't do it right you run into a
problem reminiscent to generating unique has numbers)

Hope this helps,
Michael Crutcher
Texas A&M Universisty

"Dave" <makinis3@hotmail.com> wrote:
>
>I was wondering how do i go about generating unique random numbers?
>
>for example:
>
>i want to generate 5 numbers between 1-10. i want no repeating numbers to
>occur.
>
>
>thanks to anyone who can help.