0

I was reading CLRS and I was stuck at question 5.1-2.

Describe an implementation of the procedure RANDOM(a,b) that only makes calls to RANDOM(0,1).What is the expected running time of your procedure, as a function of a and b?

The solution written here provides a complexity of O(lg(b-a)).

http://sites.math.rutgers.edu/~ajl213/CLRS/Ch5.pdf

I have too written an algorithm and I want some advice regarding it.

Random[a,b]
arr[]={a,....,b}
if(high-low <= 1)
 if(Random[0,1])
  new_arr[high]
  return
 else
  new_arr[low]
  return

if(Random[0,1])
 new_arr[a+b/2,.......,b]
else
 new_arr[a,...........,a+b/2-1]

My solution is Divide and Conquer based and that too with complexity O((b-a+1)).

Is my solution correct please tell?

OmG
  • 18,337
  • 10
  • 57
  • 90
  • 1
    You'll have better luck on `codereview.stackexchange.com`. – npinti Aug 23 '19 at 11:28
  • I'll try there as well.TIA – Arpit Pandey Aug 23 '19 at 11:32
  • The intended algorithm seems to not work properly when the length of the array is not a power of two. In such a situation some numbers will be sampled more often than others. – danbanica Aug 23 '19 at 11:51
  • Can you state an oversampled example I can not get it.I think there is no oversampling since I took care of it myself using mid-1 and mid.@qwertyman – Arpit Pandey Aug 23 '19 at 11:56
  • Here's a dotnetfiddle that demonstrates the problem with distribution - https://dotnetfiddle.net/QlBulo in a not-a-power-of-2 number of values, some values will occur more frequently than others. – Lasse V. Karlsen Aug 23 '19 at 12:19

1 Answers1

2

There is a problem when b-a+1 is not a power of two (if I understand your code correctly, but there is stuff missing in it).

Consider for example the array [1,2,3]. You have a 0.5 probability of going to [1] and a 0.5 probability of going to [2,3]. That means that in the end, the probability of picking each number is :

  • 1 : 0.5
  • 2 : 0.25
  • 3 : 0.25

Even if you divide you array as [1,2] / [2,3] the probabilities won't be correct, you'll have

  • 1 : 0.25
  • 2 : 0.5
  • 3 : 0.25
Tassle
  • 563
  • 3
  • 9
  • Probably you are incorrect because the probability of RANDOM[0,1] returning 0 or 1 is 0.5 and in the next function call the array content will only be [2,3] and the probability of both of their occurences is 0.5 only.I have used D&C technique, or perhaps probability will vary with each unique function call ? – Arpit Pandey Aug 23 '19 at 11:52
  • 2
    I know that RANDOM[0,1] has a probability of 0.5. What I am talking about is the total probability. How do you get 1? You go [1,2,3] -> [1]. This happens with probability 0.5 as you said. How do you get 3? You go [1,2,3]->[2,3]->[3]. Notice that there are two steps that need to happen, both steps happen individually with probability 0.5, so the probability that both steps happen together is 0.25. Because you need RANDOM[0,1] to return 1 twice in a row. – Tassle Aug 23 '19 at 11:57
  • Ok I am getting your point but will this algorithm produce random numbers between a and b as it is intented, if we leave probablistic analysis for a moment? – Arpit Pandey Aug 23 '19 at 12:05
  • I don't understand the point of having an array, and what `high` and `low` means. Does "array" mean that I can feed it the values 1,2,3,3,3,3,4 and it should pick a random value from that list, where 3 should occur 4 times more often than the others? – Lasse V. Karlsen Aug 23 '19 at 12:06
  • It is an algorithm and array signifies a temporary storage and yes the target here is to return random numbers that too from a to b.@LasseVågsætherKarlsen – Arpit Pandey Aug 23 '19 at 12:08
  • 1
    @Arpitking It will. But it is specified in the book that "A call to RANDOM(a; b) returns an integer between a and b,inclusive, with each such integer being equally likely", so this wouldn't fit the description of the function. – Tassle Aug 23 '19 at 12:08
  • @LasseVågsætherKarlsen : It is supposed to be a divide and conquer type algorithm (think binary search) but the way it is written is indeed incomplete and confusing. – Tassle Aug 23 '19 at 12:10
  • @Tassle Yes got your point your are right probably, but if we ignore this constraint will the algorithm produce the desired random numbers? – Arpit Pandey Aug 23 '19 at 12:11
  • If we remove the array, and only consider a/b as boundary values, then yes, it should produce values from a to b inclusive. I just tested it with a C# implementation. But as @Tassle states, the probabilities are wrong. It is not equaly likely that each number can be produced. – Lasse V. Karlsen Aug 23 '19 at 12:12
  • @LasseVågsætherKarlsen Yes I got it I will work on probability more and thanks for the code testing. – Arpit Pandey Aug 23 '19 at 12:14
  • Yeah I agree and rolled back to the previous edit. By the way, don't I have to approve edits? Because I didn't approve anything in this instance. – Tassle Aug 24 '19 at 10:50
  • @Arpitking please refrain from making [edits such as this one](https://stackoverflow.com/review/suggested-edits/23870052). They do not add any value to the site. I'm surprised it was approved in the first place. – Zoe Aug 24 '19 at 10:52
  • @Tassle as for you, as the OP of the post, you can override edit review approval/rejection. I think it might be too late now, but you'll see a "reject" button in a completed edit review that's been approved. For approval, 2k users can make edits without any review. Users with less than 2k rep need to have their edits reviewed. As the OP of a post, you have a vote that enables single-vote rejection or approval (if a user with less than 2k rep adds a suggested edit, you can reject or approve with a single vote, and as I mentioned earlier, you can also change the review outcome after completion). – Zoe Aug 24 '19 at 10:54
  • Rolling back is also fine though, but it doesn't affect the review outcome. You should also get a notification when a suggested edit has been submitted on one of your posts, which points you to review. There's also some times a notification when your post has been edited, but I don't know the requirements off the top of my head. – Zoe Aug 24 '19 at 10:55
  • Thanks for the info. I didn't know that I could reject the edit once it had been approved by other people (I did get a notification but ignored it as I was going to sleep, intending to look at it today) – Tassle Aug 24 '19 at 11:28
  • 1
    @Zoe Sorry for my mistake I am only an undergrad that too less than 2 years experience, I am only here for learning neither for rep nor any points.But yes I will abide by the rules to make it a better website. – Arpit Pandey Aug 24 '19 at 13:28