ForLoop Performance / Algorithm question

B

BlueBall

What is the most effective way, fastest way to fill up a String with data?
I wrote the following code, any suggestions on how to improve it?

StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));

Thanks..
 
J

Jezuch

U¿ytkownik BlueBall napisa³:
What is the most effective way, fastest way to fill up a String with data?

String? With DATA?
I wrote the following code, any suggestions on how to improve it?

StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));

Then you don't mean character data and you don't want to put it in a
String[1]. What do you *really* want?

[1] it's sooooo C++-ish
--
Ecce Jezuch
"But it's not real and that's why its how I always want to feel
so let's die, before the secret gets revealed
I've tried but nothing ever can appeal
and if you don't mind I'd like to throw it all away" - J. Stem
 
R

Roedy Green

StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));
try:

Random wheel = new Random();
char [] str = new char[ 10000 ];
for (int i=0; i<10000; i++)
{
str = wheel.nextInt( 255 ) /* 0..254 */ +1 ;
}
// convert to string if you must.
see http://mindprod.com/converter.html

your style of generating random ints can fail sometimes. See
see http://mindprod/jgloss/gotchas.html#RANDOM
 
B

Bernhard Pfahringer

What is the most effective way, fastest way to fill up a String with data?
I wrote the following code, any suggestions on how to improve it?

StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));

Thanks..

don't know if that would be faster, but worth a try:

String s = new String((new BigInteger(8*length,new Random())).toByteArray());

cheers, Bernhard
 
T

Tony Morris

BlueBall said:
What is the most effective way, fastest way to fill up a String with data?
I wrote the following code, any suggestions on how to improve it?

StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));

Thanks..

char[] c = new char[10000];

// assume JIT is smart
for(int i = 0; i < c.length; i++)
{
c = ((char)(Math.random() * (254)) + 1);
}

String s = new String(c);

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
C

Chris Uppal

BlueBall said:
StringBuffer str = new StringBuffer();
for (int i=0; i<10000; i++)
str.append((char) ((int) ((Math.random() * (254)) + 1) ));

Leaving aside the weirdness of what you are doing...

1) Create the StringBuffer with the necessary capacity in advance:

StringBuffer str = new StringBuffer(10000);

Tony Morris's suggestion might well be even faster, though, providing you don't
mind creating the extra array.


2) Do you /really/ need the data to be so random ? You may be happy with:

StringBuffer str = new StringBuffer(10000);
char[] c = new char[100];
for (int i = 0; i < c.length; i++)
c = ((char)(Math.random() * (254)) + 1);
for (int i = 0; i < 100; i++)
str.append(c);

which generates a lot less random data, and so should be faster (I haven't
measured it).


3) Come to that, do you need /random/ data at all ? Perhaps you could just
fill in c[] (above) with:

for (int i = 0; i < c.length; i++)
c = (char)i;


But I still think this is a really strange thing to want to do. Or at least to
want to do /and/ care how fast it is...

-- chris
 
R

Roedy Green

I would certinly agree that his method for generating random
numbers is inferior to using Random.nextInt( int ) due to it
being slower and less random, but I think you are overstating the
case to say that it could "fail".
read the notes on how that technique, though not necessarily that
particular piece of code, can sometimes fail to give you the expected
results. see http://mindprod.com/jgloss/randomnumbers.html
 
L

Liz

Dale King said:
Hello, Roedy Green !


I would certinly agree that his method for generating random
numbers is inferior to using Random.nextInt( int ) due to it
being slower and less random, but I think you are overstating the
case to say that it could "fail".

If you want the best random numbers you can buy a cd-rom and read
them from a file.
 
D

Dale King

Hello, Roedy Green !
You said:
On Tue, 11 May 2004 17:50:15 GMT, "BlueBall"
));

your style of generating random ints can fail sometimes. See
see http://mindprod/jgloss/gotchas.html#RANDOM

I would certinly agree that his method for generating random
numbers is inferior to using Random.nextInt( int ) due to it
being slower and less random, but I think you are overstating the
case to say that it could "fail".

I know you once claimed that there were cases where
Math.random()*n could sometimes evaluate to n, but Patricia the
floating point goddess debunked that theory years ago.
 
D

Dale King

Hello, Roedy Green !
You said:
read the notes on how that technique, though not necessarily that
particular piece of code, can sometimes fail to give you the expected
results. see http://mindprod.com/jgloss/randomnumbers.html

I think you meant to point me to the gotchas link since that link
only talks about pseudo-random vs. true random.

The gotchas link basically still has the same nonsense about how
Math.random()*n can sometimes evaluate to n. However that
assertion simply has no basis in reality. If you still think it
is true then you should be able to demonstrate it using an
integer and a double in the range of [0,1) whose product is not
less than the integer. You claim there are such values. Patricia
assured you there cannot be such values. If there are such
values, then reproduce them
 
D

Dale King

Hello, Liz!
You said:
If you want the best random numbers you can buy a cd-rom and read
them from a file.

Sorry, that has nothing whatsoever to do with the question I
raised with Roedy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top