The Random class
Two questions regarding the Random class in java.util package.
1) Is there a way to exclude specific integers from a SINGLE generator declaration? I want to create a program that will create random 3 or 4 digit numbers, while excluding 8s and 9s.
2) Is there a way to specify how many digits must be in the generated integer? i.e. I want a four digit number that can be from 0000-9999.
Any assistance would be GREATLY Appreciated.
[457 byte] By [
mwill20a] at [2007-11-26 16:41:35]

> 1) Is there a way to exclude specific integers from a
> SINGLE generator declaration? I want to create a
> program that will create random 3 or 4 digit numbers,
> while excluding 8s and 9s.
No.
> 2) Is there a way to specify how many digits must be
> in the generated integer? i.e. I want a four digit
> number that can be from 0000-9999.
0000 is not an integer number to start with. You want four random digits from 0 to 7, concatenated to a String.
> 1) Is there a way to exclude specific integers from a
> SINGLE generator declaration? I want to create a
> program that will create random 3 or 4 digit numbers,
> while excluding 8s and 9s.
In this case it may be easiest to draw the 3 or 4 random digits (ints between 0 and 9) one by one. If a happens to be an 8 or a 9 you just draw a new one until you have the required number of digits. Then you build the 3 or 4 digit number from the digits.
> 2) Is there a way to specify how many digits must be
> in the generated integer? i.e. I want a four digit
> number that can be from 0000-9999.
In this case you just draw a random int in that range.
Note that if you get say a 0 it's the same as 0000, and 13 is 0013. It's just a question of formatting the text version of the int.
How does one go about doing this?
> How does one go about doing this?Read the first reply again. What do you not understand?
How do you assign "... random digits from 0 to 7, concatenated to a String."?
> How do you assign "... random digits from 0 to 7,> concatenated to a String."?Which one? Do you know how to use the Random class?Or are you asking how to concatenate a String?
How do I format the program so that 0 reads the same as 0000? I suppose that that should have been my second question.
generate 4 random numbers between 0 and 7 if you dont want 8 and 9 and then put them in a stringieint x=randomNumber; String s=""+x+x1+x1+x2under stand the logic?
Concatenating a string doesn't exactly solve the problem that I have. I am trying to use the least amount of coding necessary and performing a string concat adds to the coding.
Something like this, perhaps:
package cruft;
import java.util.Random;
/**
* Given a universe of characters, generate a random String of
* given length
* Date: Jan 30, 2007
* Time: 8:27:21 AM
*/
public class RandomStringGenerator
{
public static final int DEFAULT_LENGTH = 4;
public static final String DEFAULT_CHARS = "01234567";
public static void main(String[] args)
{
RandomStringGenerator generator = new RandomStringGenerator();
for (int i = 0; i < 10; ++i)
{
System.out.println(generator.generate(DEFAULT_CHARS, DEFAULT_LENGTH));
}
}
public String generate(String values, int length)
{
StringBuilder builder = new StringBuilder(length);
char[] chars = values.toCharArray();
Random random = new Random();
for (int i = 0; i < length; ++i)
{
builder.append(chars[random.nextInt(chars.length)]);
}
return builder.toString();
}
}
%
> How do I format the program so that 0 reads the same
> as 0000? I suppose that that should have been my
> second question.
You don't need to do that! Please try out the solution Ceci provided. Then you might understand it better.
Again, no formatting needed.
is this for a school project? and you want us to do it in the least amount of code then i am sorry i am out...............now on the other hand if this solved your problem then say thank you! also you DONT get a NUMBER that starts with a 0 thus making it txt
Thanks for your help everyone.... I appreciate it.
Here's the C version,
int r = (int)(Math.random()*0x10000);
String s = "" + ('0'+(r>>9)&7) + ('0'+(r>>6)&7) + ('0'+(r>>3)&7) + ('0'+r&7);
System.out.println(s);
It generates 4 digit strings with random digits between 0 and 7.
Hint: think octal.
at 2007-7-21 16:48:27 >

> Here's the C version,
> > int r = (int)(Math.random()*0x10000);
> String s = "" + ('0'+(r>>9)&7) + ('0'+(r>>6)&7) +
> ('0'+(r>>3)&7) + ('0'+r&7);
> System.out.println(s);
>
> It generates 4 digit strings with random digits
> between 0 and 7.
>
> Hint: think octal.
That'll be helpful for a Java developer.
Is this the winner in a code obfuscation contest?
%
Well it should beint r = (int)(Math.random()*(1<<12)); sorry.
> That'll be helpful for a Java developer.
When I said C version I mean low-level. The code I posted is Java.
I've never used octal before and I see now that 010000 was the number I wanted (not 0x10000) but 1<<12 will also do.
Isn't this a kind of a trap,
int i = 010000;
The number assigned is actually 4096 decimal.
> Brilliant.> > %Spoken by a truely mediocre who can just stand and stare in awe. Let's hear you say it now. - I would never consider that code because it's not OO. What a loser you are duffymo.
Wow.You know you should get a life if you say to someone "you're a loser, you don't use OO".
> Isn't this a kind of a trap,
>
> int i = 010000;
>
> The number assigned is actually 4096 decimal.
What's so strange about it? I've seen threads where people have asked why code like this doesn't compile:
private static final int BLAH_1 = 01;
private static final int BLAH_2 = 02;
//Some more...
private static final int BLAH_08 = 08;
private static final int BLAH_09 = 09;
private static final int BLAH_10 = 10;
Kaj
kajbja at 2007-7-21 16:48:27 >

> > Isn't this a kind of a trap,
>
> What's so strange about it?
I've never used an octal literal before and I guess most haven't. 10 is ten but 010 is eight.. That syntax isn't quite what one would expect so I thought it might be considered a trap (or at least an SCJP question :)
kajbja at 2007-7-21 16:48:27 >

> Spoken by a truely mediocre who can just stand and stare in awe.
(shrug)
> Let's hear you say it now. - I would never consider
> that code because it's not OO.
No, I wouldn't consider that code because (1) the OP asked for a Java solution, (2) it's unnecessarily obfuscated and unreadable, and (3) any efficiency gain that you manage by using low level operators is lost by the difficulties in maintaining the code.
> What a loser you are duffymo.
Compared to you? Hardly. On any scale, I'm happy.
Is this all you have to offer, UJ? C code? Bitter diatribes? And I thought this was a technical forum.
You'll spout for a few weeks until your nic is closed out or the shame forces you to withdraw. You'll dream up another incarnation and come back. Rinse, repeat. I've seen it many times.
It's a measure of your personal shame that you can't even maintain an identity here.
%
> Is this all you have to offer, UJ? C code?What C code? Are you thick or what!#19 #19 #19 #19 #19 #19 #19 #19 #19
Guess so. Mea culpa, UJ, and my best to your horse Embla.%
> Guess so. Mea culpa, UJ, and my best to your horse> Embla.I don't have a horse. Didn't you read my open letter to Dr. Phil?
> > Guess so. Mea culpa, UJ, and my best to your> horse> > Embla.> > I don't have a horse. Didn't you read my open letter to Dr. Phil?Nope. Missed it.%