6 Replies - 2410 Views - Last Post: 01 November 2011 - 07:45 AM Rate Topic: -----

#1 smitha.r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-September 11

I need non repeating random numbers

Posted 26 October 2011 - 05:58 AM

I have got repeating random numbers.
import java.lang .*;
import java.util.Random;
public class RandomNumberBounds {

    private static void doRawRandomNumber() {

        int rawRandomNumber;
        int min = 32;
        int max = 256;

        for (int i = 0; i < 500 ; i++) {
            rawRandomNumber = (int) (Math.random() * (max - min + 1) ) + min;
            System.out.println("Random number : " + rawRandomNumber);
        }

        System.out.println("\n");
    }


    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     */
    public static void main(String[] args) {
        doRawRandomNumber();
    }

}


using gregorian calender is it possible to get non repeating random numbers.
using the current time and date in nano second.

Is This A Good Question/Topic? 0
  • +

Replies To: I need non repeating random numbers

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: I need non repeating random numbers

Posted 26 October 2011 - 06:33 AM

Kind of defeats the purpose of randomness, doesn't it?

You may be able to program your own solution if you think of what you want as a group of randomly selected objects from a set of unique objects.
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,191
  • Joined: 11-December 07

Re: I need non repeating random numbers

Posted 26 October 2011 - 06:33 AM

What does a gregorian calendar have to do with random numbers, unless you need random dates? Here are two really simple ways of generating non-repeating random numbers. They both use sets.

1. Start with an empty set. The set will eventually contain all the numbers you have already chosen. Choose numbers at random in the normal fashion. If the number is in the set, discard it and choose again. If it is not in the set it's one you haven't already chosen. Add it to the set and return it.

2. Start with your set full of all the possible numbers. This is great for ranges like 0-100 but terrible for large ranges. When you need to choose a number at random, remove a random one from the set and return that.
Was This Post Helpful? 3
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: I need non repeating random numbers

Posted 26 October 2011 - 12:58 PM

Like for a Loto with non-repeating numbers ?

http://www.dreaminco...snippet2992.htm
Was This Post Helpful? 2
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: I need non repeating random numbers

Posted 26 October 2011 - 01:02 PM

Your random number is between 32 and 256? Wont it have to repeat after 224 numbers?

If you have a finite set, then it's essentially shuffling cards. Make a list of all the numbers in the set and then shuffle it. Pick as many as you like off the top.

Alternately, make a list of the values you've already chosen and make sure the one you just chose isn't there. If it is, try again.
Was This Post Helpful? 1
  • +
  • -

#6 smitha.r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-September 11

Re: I need non repeating random numbers

Posted 01 November 2011 - 07:30 AM

 import java.util.HashSet;
import java.util.Set;
import java.util.*;


public class NRand {
	/**
	 * For testing
	 */
	public static void main(String[] args) {
	    // Return 6 unique numbers between 1 and 40
		System.out.println(NRand.nUniqueRandom(100, 1,150));
	}

	/**
	 * Produce a Set of 'howMany' random integers in the range
	 * between 'lowerBound' and 'upperBound' inclusive
	 *
	 * @param howMany How many numbers do we want?
	 * @param lowerBound The lower bound of the range (inclusive)
	 * @param upperBound The upper bound of the range (inclusive)
	 *
	 * @return A Set containing integers
	 */
	public static Set<Integer> nUniqueRandom(int howMany, int lowerBound,
		int upperBound) {
		Set<Integer> numbers = new HashSet<Integer>(howMany);

		while (numbers.size() < howMany) {
			numbers.add((int) (Math.random() * (upperBound - lowerBound + 1)) +
				lowerBound);
		}
 
		return numbers;
	}
}


How to shuffle the random numbers in this code.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: I need non repeating random numbers

Posted 01 November 2011 - 07:45 AM

You can't shuffle a Set. Plus you are already filling it with Random numbers. If you want to shuffle, you should use a List instead, and you can use the Collections.shuffle() method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1