John Smith
John Smith

Reputation: 6269

Generate Random with Substring

Let's say I have a string main_string = "germany"

And now I want to generate random strings that contain this main_string

The results should be for example:

 germanyXv43
 Fggermany3s
 germany55FR

Whats the best way to do this?

I thought in using SecureRandom

For example:

30000.times do 
   "germany" + SecureRandom.hex(3)
end 

But as you can guess then germany would be always at the beginning of the string:

germany8s5
germanyDF4
......

How can I do it more Random? Thanks!

Upvotes: 1

Views: 226

Answers (1)

shivam
shivam

Reputation: 16506

Create an Array and shuffle it:

(["germany"] + SecureRandom.hex(3).chars).shuffle.join
# => "cgermany06f96"
# => "70efgermanyb4"
# => "germany934732"
# => "ebgermany9e4f"

Upvotes: 6

Related Questions