How to create a random number without an 8 & 9?

Hi I am trying to make random phone numbers but for the area code I don't want any 8 or 9. How can I do it?

Here is my code:

publicstaticvoid main(String[]args)

{

//declaration section

Random generator =new Random();

int areacode,num1, num2;

//generate phone digits

areacode= generator.nextInt(700);//areacode it can't have an 8 or 9

num1= generator.nextInt(643)+ 100;//second set

num2= generator.nextInt(9000)+ 1000;//third set

//Decimal Format

DecimalFormat fmt =new DecimalFormat ("#000");

System.out.print("Phone Number:" + fmt.format(areacode)+"-" + num1 +"-" + num2);

}

}

[1254 byte] By [Bijou03a] at [2007-10-3 6:32:40]
# 1
Help please...
Bijou03a at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 2

A math guru like Jos might have a better way but you could write a method.

private int getAreaCode(limit) {

while true {

generate number

if number doesn't contain an 8 or a 9

return number

}

}

}

floundera at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 3
> Help please...In future do you think you can wait more than 10 minutes before whining?
floundera at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 4
Sorry
Bijou03a at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 5
Is there another way of doing this?
Bijou03a at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 6
Yes. In programming there are plenty of ways to achieve the same result. Care to explain what is so bad about my solution.
floundera at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 7

I don't like the idea of discarding attempts with the wrong digits and then trying again, because there is a small but finite probability that the loop will never end.

I would do it by generating random ints from 0-7 three times and adding that to the total, multiplying by ten each time to put them in the right position.

Mr_Evila at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 8
yes I will generate each separately....
Bijou03a at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 9
Or even better append them to a StringBuffer.
floundera at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 10
thanks for your help but I still haven't learn how to use StringBuffer.
Bijou03a at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 11
If you haven't been told you can't use it then learn it now. I'm sure the teacher won't mark you down if you do.
floundera at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 12
java.util Class Random int nextInt(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
mchan0a at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 13

>> I don't like the idea of discarding attempts with the wrong digits and then trying again, because there is a small but finite probability that the loop will never end.

Thats not true. Computer random isnt real random - meaning, a number without an 8 or 9 WILL come up eventually.

In real random you can be flipping a coin and get heads for the rest of your life - that wont happen with computer random.

This may not be a perfect solution but:

int answer;

int[] valid = {0, 1, 2, 3, 4, 5, 6, 7}

for(int i = digitCount: i > 0; i--){

int random = Random.nextInt(8);

int digit = valid[random];

answer += (random * pow(10, i))

}

TuringPesta at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 14
woops, because you are excluding 8 and 9 (which are at the end) you dont need the valid array.That would work well though for cases where you were excluding say 2 and 6 or something.
TuringPesta at 2007-7-15 1:20:08 > top of Java-index,Java Essentials,New To Java...
# 15

> Thats not true. Computer random isnt real random -

> meaning, a number without an 8 or 9 WILL come up

> eventually.

But that depends on how the random number generator is implemented. It may be true in Java now, but I certainly wouldn't want to write any code that depended on that fact. And even if you could be certain that all numbers would come up eventually no matter what, it might be only after an unacceptably long time in the worst case.

Mr_Evila at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 16

>> it might be only after an unacceptably long time in the worst case.

agreed. i can see it taking too long to cycle.

>> But that depends on how the random number generator is implemented.

not to argue because this really isnt in my body of knowledge but

isnt ANY rand#gen - and at least java's - attempting to be evenly

distributed?

i think i can argue that it wouldnt be if it were possible to cycle

without getting a number with an 8 or 9 in it. : )

TuringPesta at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 17
Why can't the area code have an 8 or a 9? Your phone numbers are in the format of U.S. phone numbers, and real U.S. area codes have both.
doremifasollatidoa at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 18
1-800-IFO-UND1
TuringPesta at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 19
non-business numbers?> Why can't the area code have an 8 or a 9? Your phone> numbers are in the format of U.S. phone numbers, and> real U.S. area codes have both.
mchan0a at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 20

> non-business numbers?

> > Why can't the area code have an 8 or a 9? Your

> phone

> > numbers are in the format of U.S. phone numbers,

> and

> > real U.S. area codes have both.

Yes:

Area Code, Region, Timezone Offset from UTC, Description

949 CA -8 California: S Coastal Orange County

818 CA -8 S California: Los Angeles: San Fernando Valley

786 FL -5 SE Florida, Monroe County

682 TX -6 Texas: Fort Worth areas

And many others:

http://www.cs.ucsd.edu/users/bsy/area.html

doremifasollatidoa at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 21
>> http://www.cs.ucsd.edu/users/bsy/area.html Haha...Without 8 or 9 who would call upper central Michigan!!!989MI -5 Upper central Michigan: Mt Pleasant, Saginaw
TuringPesta at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 22

> not to argue because this really isnt in my body of

> knowledge but

> isnt ANY rand#gen - and at least java's - attempting

> to be evenly

> distributed?

> i think i can argue that it wouldnt be if it were

> possible to cycle

> without getting a number with an 8 or 9 in it. : )

That's probably true of any psuedo-random number generator, but it's not hard to get what should be a truly random number by measuring something physical, like the noise of a resistor on the motherboard (random enough that you could theoretically go all day without seeing a given number, anyway). I don't think any normal computers have such a feature as standard today, but it might be common tomorrow.

So you're probably right that it's not a problem that would crop up now, but it seems like a good idea to think ahead (don't want any Y2K-type problems cropping up!).

Mr_Evila at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 23

> but it's not hard to get what should be a truly random number by

> measuring something physical, like the noise of a resistor on the

> motherboard

Alan Turing apparently built physically indeterminate hardware into his

machines, and considered this part of "computing". Or at least so I

remember reading - historians of the art will no doubt correct me if I'm

wrong.

I have seen commercial cards for sale on trhe Internet which

provide sources of random bits at a good bandwidth provided by

radioactive sources. (No mention of whether they keep your coffee

warm as well.) And a web site - googled it, it's here:

http://www.fourmilab.ch/hotbits/hardware.html - offers online hot bits

at the modest rate of 100bps. The hippies at www.random.org

use atmospheric noise.

Is there really any difference between these sources of the (scientists

believe) physically indeterminate behaviour and the likes of say

Math.random()?

If you think so, I hope you'll behave nicely towards the next poster

who wants the "correct" answer to their floating point problems.

pbrockway2a at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 24
You could generate *any* integral random number, convert it to itsoctal string representation and convert it back to an int again as if itwere a decimal representation of a number.kind regards,Jos
JosAHa at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...
# 25
edit: This one was ****. Forget it.
Mongera at 2007-7-21 11:34:32 > top of Java-index,Java Essentials,New To Java...