generating unique random numbers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: generating unique random numbers

  1. #1
    Dave Guest

    generating unique random numbers


    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.

  2. #2
    Paul Clapham Guest

    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.




  3. #3
    Brent Worden Guest

    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.

    This tends to be faster than the process Paul described.

    Brent Worden
    http://www.brent.worden.org/




  4. #4
    Michael Crutcher Guest

    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.

    int numToTake = 5;
    int range = 10;

    Vector randomNumberSource = new Vector(range);

    // fill the source with the range of values

    for (int i; i < range; i++)
    randomNumberSource.elementAt(i) = i;


    int[] randomNumberHolder = new int[numToTake];

    for (int i; i < numToTake; i++) {

    int selectedInt = (int) Math.random() * range;

    randomNumberHolder[i] =
    (int)randomNumberSource.elementAt(selectedInt);

    // 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.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center