Math related question

Just starting out? Need help? Post your questions and find answers here.
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Math related question

Post by milan1612 »

I came across a job interview question some time ago that got me thinking.

Given a function that returns a random integer within the range between 1 and 5,
write a function that uses this random() function and returns a random integer
between 1 and 7.

So something like this:

Code: Select all

Procedure Random5()
  ProcedureReturn Random(4) + 1
EndProcedure

Procedure Random7()
  ; magic happens here, using only the above function
EndProcedure
I have absolutely no idea how this could be done, do you?
Windows 7 & PureBasic 4.4
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Re: Math related question

Post by Matt »

srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Math related question

Post by srod »

Here's one I just knocked up.

Code: Select all

Procedure Random5()
  ProcedureReturn Random(4) + 1
EndProcedure

Procedure Random7()
  Repeat
    row = Random5()
    col = Random5()
    total = (row-1)*5 +col - 1
  Until total <= 20
  ProcedureReturn total / 3 + 1
EndProcedure

For i = 1 To 100
  Debug Random7()
Next
All I do is use two random numbers between 1 and 5 to produce a random number between 0 and 24 (the value of total in the above code). Repeat until you actually get something between 0 and 20 (which gives 21 numbers; a multiple of 7). Then, if the resulting value (total) is between 0 and 2 we return a result of 1. If the value is between 3 and 5 then we return a result of 2 and so on. This gives us a result between 1 and 7; each of which is equally likely.
I may look like a mule, but I'm not a complete ass.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: Math related question

Post by citystate »

Code: Select all

Procedure.l Random7()
  result.l = 0
  For i = 1 To 7
    result + Random5()
  Next i
  ProcedureReturn result/5
EndProcedure
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
STARGÅTE
Addict
Addict
Posts: 2267
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Math related question

Post by STARGÅTE »

sry citystate thats no right random:

Code: Select all

Procedure Random5()
  ProcedureReturn Random(4) + 1
EndProcedure

Procedure.l Random7()
  result.l = 0
  For i = 1 To 7
    result + Random5()
  Next i
  ProcedureReturn result/5
EndProcedure 

Dim Test(7)

For n = 1 To 70000
 i = Random7()
 Test(i) + 1
Next

For n = 1 To 7
 Debug Test(n)
Next
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
idle
Always Here
Always Here
Posts: 6103
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Math related question

Post by idle »

What gets me is that they still ask these illogical questions at interviews!

[edit] 1 to 7 [/edit]

Code: Select all

Procedure Random5()
  ProcedureReturn Random(4) + 1
EndProcedure

Procedure Random7()
  ;common multiple 5*7 
  For a = 1 To 7
    b + Random5()
  Next 
  b % 7 + 1 
  ProcedureReturn b 
EndProcedure  

Dim ct(7)

For a = 1 To 10000 
  x = random7()
  ct(x)+1
Next   

For a = 1 To 7 
 Debug StrF((ct(a)/10000)*100.0,3) 
Next 
;Even odds 14.28% 
Last edited by idle on Mon Dec 07, 2009 8:17 am, edited 1 time in total.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: Math related question

Post by citystate »

in that case STARGÅTE,

Code: Select all

Procedure random7()
  result.l = 0
  ;generate a number between 0 and 20 ( 5 x [0 to 4] )
  For i = 1 To 5
    result + (random5() - 1)
  Next i
  ProcedureReturn result/3 + 1
EndProcedure
:D
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
idle
Always Here
Always Here
Posts: 6103
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Math related question

Post by idle »

no that don't work either the odds are all skewed
I fixed your solution with a mod
User avatar
Michael Vogel
Addict
Addict
Posts: 2824
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Math related question

Post by Michael Vogel »

Why you're not doing a sum using seven calls of Random(5) each time?

Code: Select all

Procedure Random7()
	;common multiple 5*7
	For a = 0 To 6
		b + Random5()
	Next
	b%7+1
	ProcedureReturn b
EndProcedure
And, if the random numbers don't have to be more than (very :lol:) pseudo numbers, what's about something like this:

Code: Select all

Procedure Random7()
	ProcedureReturn (Random5()+3)%7+1
EndProcedure

PS edited, bold tags () in code do not work...
Last edited by Michael Vogel on Mon Dec 07, 2009 7:42 am, edited 3 times in total.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: Math related question

Post by citystate »

the question didn't ask for an even probability spread, just a function that would produce a random number between 1 and 7 8)
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
idle
Always Here
Always Here
Posts: 6103
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Math related question

Post by idle »

Michael Vogel wrote:Why you're not doing a sum using seven calls of Random(5) each time?

Code: Select all

Procedure Random7()
	;common multiple 5*7
	For a = 0 To 6
		b + Random5()
	Next
	b%7+1
	ProcedureReturn b
EndProcedure
And, if the random numbers don't have to be more than (very :lol:) pseudo numbers, what's about something like this:

Code: Select all

Procedure Random7()
	ProcedureReturn (Random5()+3)%7+1
EndProcedure

PS edited, bold tags () in code do not work...


That doesn't work!

The common multiple is 35 so you need to do random5() seven times
and then mod it

A random distribution is supposed to be a normal distribution
all city states function needed was a mod to make it work properly
User avatar
Michael Vogel
Addict
Addict
Posts: 2824
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Math related question

Post by Michael Vogel »

idle wrote:That doesn't work!
The common multiple is 35 so you need to do random5() seven times
and then mod it
Hi, I've really thought this are seven times... :shock:

Code: Select all

Procedure Random5()
	ProcedureReturn Random(4)+1
EndProcedure

Procedure Random7()
	;common multiple 5*7
	For a = 0 To 6
		Debug Str(a+1)+" time"+Mid("s",1,a)
		b + Random5()
	Next
	b%7+1
	ProcedureReturn b
EndProcedure
Debug Random7()
idle wrote:A random distribution is supposed to be a normal distribution
Yes, of course (should have added some more :lol: above) - otherwise all these things would be correct:

Code: Select all

Procedure Random7()
	CompilerSelect #RandomType
	CompilerCase #AbsolutePseudo
		ProcedureReturn 2
	CompilerCase #Simple
		ProcedureReturn Random5()
	CompilerCase #SimpleAndCentered
		ProcedureReturn Random5()+1
	CompilerCase #Crazy
		ProcedureReturn (ElapsedMilliseconds()*Random5())%7+1
	CompilerEndSelect
EndProcedure
User avatar
idle
Always Here
Always Here
Posts: 6103
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Math related question

Post by idle »

sorry I see what you mean now!
I had 0 to 7 while I was meaning 1 to 7, though 0 to 6 would suffice
well I did at least kind of explain the reason.
User avatar
Michael Vogel
Addict
Addict
Posts: 2824
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Math related question

Post by Michael Vogel »

idle wrote:sorry I see what you mean now!
I had 0 to 7 while I was meaning 1 to 7, though 0 to 6 would suffice
well I did at least kind of explain the reason.
You're right, I just stole your explanation (a good strategy for normal) to get the score :wink:

Michael
User avatar
idle
Always Here
Always Here
Posts: 6103
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Math related question

Post by idle »

it wasn't my strategy I would have opted for crazy :lol:
Post Reply