12 Replies - 2198 Views - Last Post: 08 February 2012 - 09:19 AM Rate Topic: -----

#1 theman1010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-February 12

random letter generator

Posted 06 February 2012 - 03:50 PM

Hi,
Im trying to generate a random letter using the letters A-F but have no idea how to input the data.
I know how to use the random generator for numbers, but when it comes to letters Im not sure what to change to make it work.
Ill give a sample of my random number code and see if any suggestions can be made on what to edit to get the letters outputted instead.

Thanks!


import java.util.Random;
 import java.text.DecimalFormat;
 
public class RandomNumber
 { 
public static void main (String[] args) 
{ 
double pi, total;
 int random;
 pi=3.14159;
 
DecimalFormat f = new DecimalFormat("0.00");
 Random generator = new Random();
 random = generator.nextInt(10)+ 1;
 
total=random * pi ;
 
System.out.println ("Random: " + random);
 System.out.println ("PI: " + pi);
 System.out.println ("Random x PI: " + f.format(total));
 
}
}

This post has been edited by smohd: 06 February 2012 - 09:37 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: random letter generator

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: random letter generator

Posted 06 February 2012 - 04:03 PM

A character (char) is just the ascii value of a certain letter. Using that hint try to figure out how to get it to generate a random letter, if you run into trouble while writing your code, someone will offer you some more help.
Was This Post Helpful? 0
  • +
  • -

#3 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: random letter generator

Posted 06 February 2012 - 04:22 PM

You can still use a random number generator if you assign each letter a number and specify that the minimum random number range is no larger than 26. It might be inefficient, but if you had to, you could make if-statements for each letter, like "if randomNum = 1, letter = A"
Was This Post Helpful? 0
  • +
  • -

#4 theman1010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-February 12

Re: random letter generator

Posted 06 February 2012 - 08:26 PM

heres what i have so far:

public class RandomNote
 { 
public static void main (String[] args) 
{ 
int random;


 Random generator = new Random();
for (int i = 0; i < 10; i++) 
{
  int random = randomLetter.nextInt(6) + 1;
  System.out.println(random);
 
total=random;
 
System.out.println ("Random note: " + random);

This post has been edited by smohd: 06 February 2012 - 09:37 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: random letter generator

Posted 06 February 2012 - 09:51 PM

The idea given by ianian112 is very simple, we know that capital letters starts from 65 to 90 in ASCII character set and they are 26 letters, then generate a number between 65 to 90 and cast it to character. That is all and you will have your letters.
From what you have, do the following changes:
- Dont start from 1, start from 65
- Cast the returned random number to character.
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: random letter generator

Posted 06 February 2012 - 10:09 PM

Better still, since we know that, to java, a char is just an int, you can make your code more legible by referring to 'A' instead of to 65. Now if you get a random number from zero to 25, inclusive, you can add it to 'A' and get a char in the range 'A' to 'Z', inclusive.
Was This Post Helpful? 1
  • +
  • -

#7 theman1010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-February 12

Re: random letter generator

Posted 06 February 2012 - 10:50 PM

Ok thanks for the responses.

So if i wanted to just refer to the letters, how would i do that?
I know I would need a String but how do I write that out for letters.
Would it be something like

int random = randomGenerator.nextInt("A", "B", "C", "D", "E", "F")
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: random letter generator

Posted 06 February 2012 - 11:03 PM

Try reading the documentation. Read especially this bit:

Quote

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.



And when you think you understand it, try to use it. Make your own mistakes for a while, it'll help.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,645
  • Joined: 20-September 08

Re: random letter generator

Posted 07 February 2012 - 04:03 AM

Another approach you could try: note that A-F are the values 10-15 in hexadecimal
Was This Post Helpful? 0
  • +
  • -

#10 theman1010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-February 12

Re: random letter generator

Posted 07 February 2012 - 10:32 AM

Thanks everyone!
Well my random number generator works and ive edited it to focus on numbers 1-10. Unfortunately im still not sure how to replace that with letters in a string
Heres what i have:

import java.util.Random;

 
public class RandomNote
 { 
  int random;
int letter;
int total;
public static void main (String[] args) 
{ 
 Random randomGenerator = new Random();
 
for (int i = 0; i < 10; i++) 
{
  int random = randomGenerator.nextInt(6) + 1;
  System.out.println(random);

int total=random;
}


System.out.println ("Random note: " );
 
}
}

This post has been edited by smohd: 07 February 2012 - 10:44 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: random letter generator

Posted 07 February 2012 - 10:51 AM

View Postg00se, on 07 February 2012 - 06:03 AM, said:

Another approach you could try: note that A-F are the values 10-15 in hexadecimal


If the assignment is to display hex, this is fine. Now you just have to work out how to get ints to display in hex, and that's just a matter of going through the basic libraries to find what you need. No coding at all involved.
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: random letter generator

Posted 07 February 2012 - 11:02 AM

Let me give you an idea and then you can arrange your numbers to satisfy the letters from ASCII list. Just try to give the output in char instead of integer like:
System.out.println((char)random);

And you will see that the it prints a character related to the generated number, and so you may get an idea
Was This Post Helpful? 0
  • +
  • -

#13 theman1010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-February 12

Re: random letter generator

Posted 08 February 2012 - 09:19 AM

this is the part Im having trouble changing as Im not sure how to adjust this to accept letters.

for (int i = 0; i < 10; i++)
{
int random = randomGenerator.nextInt(6) + 1;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1