• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random  RSS feed

 
richa parab
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I want to select a bit randomly from given string ...

1 0 1 0 1 0 1 1 1 1 1 0 1 1 0 1 0

Also want to set seed.
By using Random() function we can generate random number in given range. but i cant select random number from given binary string so can any one help me??
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the length of your binary String is say N then just create a random number in the range 0 to N-1 and then use that number as an index into the binary String!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
richa parab wrote:Also want to set seed.
By using Random() function we can generate random number in given range...

And if you use a Random (java.util.Random) object, you can do both.

but i cant select random number from given binary string

Which - if I'm understanding this right and you want to get a random digit from your binary String - seems to be a rather odd thing to want to do.

Care to explain why you're doing this?

Winston
 
richa parab
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Richard.

@Winston
I want to insert that randomly selected bit into numeric database. And to identify position where i have inserted that bit i am setting seed.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
richa parab wrote:I want to insert that randomly selected bit into numeric database. And to identify position where i have inserted that bit i am setting seed.

But that doesn't make any sense:

1. Setting the seed merely ensures that you get a sequence of random numbers that you can repeat, which can be quite useful for testing; but it doesn't alter the randomness of those numbers.

2. Selecting a random bit from a string of bits sounds to me just like calling Random.nextint(2) - ie, you'll get either 0 or 1 randomly - unless, you're using the string to determine the probability of getting a '1'. And if that's what you're doing, there are much simpler ways.

I suspect there's something about this "selecting from a string and putting in a database" that you're not explaining to us.

Winston
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you seed the RNG you probably want to keep track of that seed. When I write automated tests I seed the RNG with the current date, then print that seed to the output log file. That way if a test fails I can reseed the RNG and get the same "random" numbers I had when the test failed.

I concur with others, it sounds like either you have simplified the problem so we can understand it easily, or you're doing something wrong.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!