• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random phone number generator

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have had help with my code from another forum, the app is almost done, but no one is answering there now. I am trying to make a phone number generator. I get the following error though when trying to compile.

I posted this on another forum but no answer anymore,.


my post

C:\JAVA_PROGRAMMING_CODE\code1\RandomPhoneNum>javac RandomPhoneNum.java
RandomPhoneNum.java:23: cannot find symbol
symbol : method toOctalString(int)
location: class RandomPhoneNum
strippedNum = toOctalString(num1);
^
1 error



here is the code so far



Also, how did you people know about octal base numbers? I would never have known that. Do you know what I can do to improve my math ability please? I desperately need it. like any books you know? please. thanks.
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right: you're calling a method called toOctalString, but your code has no such method. There is a class in the Java libraries that does have such a method, though. Do you know how you can find out which class of the standard Java class libraries has a particular method? (Hint: it involves perusing the javadocs.)
 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Derek,


It should be Integer.toOctalString(num1), which converts the given number to octal format and stores in a String.

For more clarification you can go this forum

https://coderanch.com/t/513799/java-programmer-SCJP/certification/toHexString-toOctalString
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

derek smythe wrote:
Also, how did you people know about octal base numbers? I would never have known that. Do you know what I can do to improve my math ability please? I desperately need it. like any books you know? please. thanks.



Don't know about other countries, but in the U.S., converting a number from one base to another, is probably first encountered in the eighth grade. Some of my friends claim it was earlier. In my case, eighth grade sounds about right.

Henry
 
Master Rancher
Posts: 4806
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I believe it's covered in grade 1000.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's a grade? I remember doing octal numbers when in a fresher's course when I was doing my MSc > 30 years after getting my first degree. But not before.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't learned about octal numbers or numbers with a different base system in highschool at all. I remember my uncle explaining me binary when I was about 14 or so. But systems like binary and hexadecimal are fairly common in computer programming, especially with lower-level programming languages (C++, C, assembly language).

Mathematics is a huge world. If you want to improve your math skills, you'd have to know in which area of mathematics you'd want to start. One field that programmers often have to know something about is discrete mathematics. A famous and classic book about algorithms and datastructures is Introduction to Algorithms.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. also, my code outputs this now for the first 3 digits sometimes, it goes over 1000 and is supposed to only have 3 digits for the octal group. Any more help appreciated. thanks.


C:\JAVA_PROGRAMMING_CODE\code1\RandomPhoneNum>java Rando
This app prints out a random phone number.
The first set of 3 digits can't have 8 or 9 in them.
The second set of 3 digits can't be greater than 742.
Here is a random generated phone number: 1145-736-2547



Also, since I don't remember anything from school except basic math and averages and percents, should I just buy beginner math books and go from beginner, to algebra, to calculus? Please. Thank you.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nevermind. I just bought a ridiculous amount of math books. I should be good now. But I still need help with the problem above please.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Yes, I believe it's covered in grade 1000.



Mike, I'm sure you must be familiar with Tom Lehrer's song "New Math".
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I had heard it at least once before. He's before my time, mostly, but well worth a listen.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're generating decimal numbers up to 699, but those can have 4 digits in octal. You need to restrict the decimal numbers to those that have a 3-digit octal representation.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Here is the answer, someone helped me, but here it is if anyone is curious, you have to put in the following code.

num1= generator.nextInt(0700)+0100;// note the octal notation
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is an interesting solution to a requirement -- but it doesn't seem to be very flexible. For example, what if the phone company changes it to allow the 9 as the third digit?

Henry
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I would be in trouble. LOL!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.Random;

public class RandomPhoneNum
{
   public static void main(String[] args)
   {
       System.out.println("This app prints out a random phone number.");
       System.out.println("The first set of 3 digits can't have 8 or 9 in them.");
       System.out.println("The second set of 3 digits can't be greater than 742.");
       
       Random generator = new Random();
       
       String strippedNum;
       int num1 = 0;
       int num2 = 0;
       int num3 = 0;
       
       num1 = generator.nextInt(600) + 100;//numbers can't include an 8 or 9, can't go below 100.
       num2 = generator.nextInt(641) + 100;//number has to be less than 742//can't go below 100.
       num3 = generator.nextInt(8999) + 1000; // make numbers 0 through 9 for each digit.//can't go below 1000.
       
        String string1 = Integer.toString(num1);
       strippedNum = Integer.toOctalString(num1);
         
       System.out.println("Here is a random generated phone number: " + strippedNum + "-" + num2 + "-" + num3);
   }

}
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am confused about why the problem needed octal numbers.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

I am confused about why the problem needed octal numbers.


The only reason I can think of is that an octal number would never have 8 or 9 in it, only 0 to 7 possible in an octal number.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

derek smythe wrote:num1= generator.nextInt(0700)+0100;// note the octal notation


This was a totally WRONG solution. Too bad nobody called out OP at the time.

Streams were not a thing yet back then but I'd probably do something equivalent to:
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Campbell Ritchie wrote:Welcome to the Ranch

I am confused about why the problem needed octal numbers.


The only reason I can think of is that an octal number would never have 8 or 9 in it, only 0 to 7 possible in an octal number.



It appears to be for the purpose of generating an Area Code, But I live in Area Code 904!

Actual US Area Code rules were originally for a 3-digit number with the second digit being 0 or 1. So this algorithm would have been doubly-invalid up until about 1990, when the Great Area Code Explosion occurred. Now Cape Canaveral has Area Code 321.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Now Cape Canaveral has Area Code 321.


I thought you might have been kidding. That's awesomely funny. Who says rocket scientists don't have a sense of humor?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I came up with using IntStream. Seems to work the way it's supposed to—but how to test that for sure though, besides just eyeballing a bunch of sample output?
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Tim Holloway wrote:Now Cape Canaveral has Area Code 321.


I thought you might have been kidding. That's awesomely funny. Who says rocket scientists don't have a sense of humor?



I think you can blame it on whatever agency took over managing Area Codes rather than the locals. Actually, it geographically overlays the older Orlando 407 code (note the old-style format), which in turn was carved out of the 305 Area Code that was originally most of the Southern half of the state and centers on Miami. Though Tampa had a different phone company than the ubiquitous AT&T, so they started out with 813.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:but how to test that for sure though, besides just eyeballing a bunch of sample output?


Actually, since the range of valid values is finite, I could probably think of a brute force way to test this.

But then again, as I've written the code, it seems to me relatively easy to read each part and reason about the correctness of it. I don't know what others think though: do you think it's easy enough to reason that the code I wrote is correct just by reading and analyzing it?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Here's what I came up with using IntStream. Seems to work the way it's supposed to


A theoretcal problem is that you might produce an awful lot of octals < 100 or > 800, before the first suitable is found. An alternative is, say:



with the slight disadvantage of a biass.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt Parker gives a simple solution in Humble Pi, Harmondsworth: Penguin (2019) suggests a way to randomise phone numbers, which I shall paraphrase, but it doesn't use octal.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short & simple.

To quote Chico Marx:

Why-a-duck
Why-a-fence
Why-a-octal

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . .
about 1990, when the Great Area Code Explosion occurred.
. . .

We had to wait until 1995 when most dialling codes (=area codes) had a 1 inserted as their second character, and (I think) mobiles (=cells) were all allocated numbers starting 07...
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:
A theoretcal problem is that you might produce an awful lot of octals < 100 or > 800, before the first suitable is found. An alternative is, say:

with the slight disadvantage of a biass.


That seems like premature optimization to me. Unless I fully understand how the stream is processed and where the bottlenecks are, I'm going to assume that there are some optimizations, such as that for findFirst(), built in to the stream processing logic that will mitigate the problem you describe. Also, I'm going to trust the documentation for Random in that the numbers it generates are relatively evenly distributed. Since the range of numbers from 100-799 is larger than [0-99,800-999], I would assume there's a slightly better chance of getting numbers in the desired range than not.

In my earlier iterations, I did use min/max and I did see that bias you're talking about where I saw a disproportionate number of 100s being generated. I don't consider a bias that can occur 3 out of 10 times a "slight" bias, which is why I decided to use filter instead. That seemed to make the bias go away, even for 100 samples.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My reasoning for saying there's a 3 in 10 chance of hitting that bias is this:

It's the first digit that would determine the occurrence of the bias of 100 for max(100, x). There's a 1 in 10 chance of hitting that bias. Similarly, there's a 2 in 10 chance of hitting 8 or 9 in which case the bias for min(799, x) would be seen. So, in all, you have a 3 in 10 chance of a bias for either 100 or 799.

With a large enough sample, we could check this programmatically.

[Waiting for @Paul Clapham to tell me I need to go back and do remedial statistics]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I tested programmatically with a sample size of 20000 numbers and 50 trials - produced an average of 12% bias for both methods with the min/max method yielding a very slightly larger bias: 12.50% vs ~12.53% bias for either 100 or 799. I'm not sure my test code is correct but preliminary results show that Piet was right in claiming a slight bias. I'll post my test code later. Have to go to work now.

It does look like I need to go back to remedial stats though
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the code I used to test:

And the results of a few test runs:

min/max: 12.727%
filter: 12.499%

min/max: 12.708%
filter: 12.510%

min/max: 12.684%
filter: 12.490%

min/max: 12.691%
filter: 12.516%

So it does appear that min/max has a slight bias but it's not as big as I thought. I think my tests are reasonably formulated but feel free to poke holes in it.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't hurry with that stats course: your reasoning is spot on.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I profiled this with Java Flight recorder a few times and initially thought the filter showed a slightly better performance but with a few more runs they both flip-flopped. I'm guessing with a large sample of runs, the filter will still come out with having a very slight advantage in performance on average but the difference is almost negligible: about 250ms either way.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Don't hurry with that stats course: your reasoning is spot on.


Thanks for the reassurance but the test results seem to belie that.

Given concrete results and confidence in my reasoning, I think the concrete results will beat my confidence into submission every time.  
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[shameless digress]
I just wrote a Stats class, that collects elements of a certain type, and that gets supplied with a Function that maps these elements to a double, to do some statistics. Pure hobbyism that is, Python and R are of course the things to use.

If we generate 3-digit numbers, with digits ranging from 0 to 7, and we set the min to 100 and the max to 742, then all numbers starting with 0 will be set to 100, and all numbers from 743 to 777 to 742. That is (64 + 29) / 512 = 0.181640625.

Running my code, I get as number of observations, average of applying min/max, variance and st. dev
100000
0,1825
0,1492
0,3862

That biass is way too high of course. Well, some code, hopefully interesting for one and a half man and a horsehead:

and I run it with

and

[/shameless digress]
 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic