sunny
sunny

Reputation: 145

Random number every time/everyday

I have a 12 digit number in which first 8 digits are fixed but last 4 digits(number) i want to be different,It gives me range(0000-9999). Problem is this script may run 2-3 times daily or different days as well. If i use random function it might happen one number which is generated today is same as tomorrow's. Any solution to get different 4 digit number every-time/everyday.

Upvotes: 1

Views: 993

Answers (2)

Nathan Marotta
Nathan Marotta

Reputation: 136

I know this may not be what you want but this is a method I made which generates a random number from the time and a couple arrays, manipulate and use it if you can :)

import java.text.*;
import java.util.*;

public class Randomizer {
public void randomizer() throws InterruptedException {
    Randomizer r = new Randomizer();
    int[] numbers = { 3, 7, 2, 62, 1, 53, 16, 563, 12, 13, 75 };
    Calendar rightNow = Calendar.getInstance();
    int hour = rightNow.get(Calendar.HOUR_OF_DAY);
    int minute = rightNow.get(Calendar.MINUTE);
    int seconds = rightNow.get(Calendar.SECOND);
    int[] numbers2 = { 10, 32, 61, 2, 5 };
    int[] date = { hour, minute, seconds };
    int RandomNumber = (r.getRandom(date) * r.getRandom(numbers)) + r.getRandom(numbers2);
    System.out.println("Random number generated: " + RandomNumber);

}

public static int getRandom(int[] array) {
    int rnd = new Random().nextInt(array.length);
    return array[rnd];
}

public static void main(String[] args) throws InterruptedException {
    Randomizer r = new Randomizer();
    r.randomizer();

}

}

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234845

You can't compress a time uniquely down to a 4 digit number so that approach can be immediately eliminated.

What you need to do is to store a count somewhere. Start from 0. Your script needs to increment that stored number, perform its task, then store the incremented value. If that value reaches 10000 then you've reached your capacity limit for your scheme.

If you need the scheme to appear random to your users, then map that consecutive scheme to a shuffled set of numbers solely for the purpose of constructing the 12 digit number.

Upvotes: 1

Related Questions