Closed Thread Icon

Topic awaiting preservation: A little exercise for all of you (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8752" title="Pages that link to Topic awaiting preservation: A little exercise for all of you (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: A little exercise for all of you <span class="small">(Page 1 of 1)</span>\

 
Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-13-2003 06:32

OK, this is intended basically as practice for beginning to intermediate programmers. If the answer comes to you right away, don't say anything; let other people work it out for themselves.

Here's the problem: You want to generate two random numbers, each between zero and nine (inclusive). However, you want to make sure that the numbers aren't the same. (If the first number is 4, you don't want the second number to be 4.) Making sure that each number has an equal probability of being chosen as either the first or second number, come up with a four line (or fewer) program which comes up with the two numbers.

There are multiple solutions, but I'm looking for one which doesn't involve a loop.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 07-13-2003 11:59

I couldn't resist, but to post my solution. It's a two line script (written in JavaScript, of course), one line for each number. Although, the script isn't "rocket science", it served as an exercise for using Math.random() function in JavaScript to generate random numbers (random as much as possible and with equal chance for all numbers to appear).

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!-- ;

// Written by mr.maX, http://www.max.co.yu/

firstnumber = Math.round(-0.5+(9.99999*Math.random()));
secondnumber = /\d$/.exec(Math.round(firstnumber+0.5+(8.99999*Math.random())))[0];

// Instead of using RegEx to get last digit, this line would be more "mathematically correct":
// if (secondnumber>=10) secondnumber = secondnumber-10;

// Output (this line shouldn't be counted, heh :-))
document.write(firstnumber + '&nbsp;' + secondnumber);

// -->
</SCRIPT>


mas
Paranoid (IV) Inmate

From: the space between us
Insane since: Sep 2002

posted posted 07-13-2003 16:54

mhm i would do that with some mathimatical functions... HEH *looksovertomax*
btw, max, since when are you posting again?

elysiumart

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-13-2003 23:53

Wow, that's pretty complicated. I think it works almost the same as the solution that I had in mind, but I'm not sure.

By the way, when implemented correctly, I'm pretty sure that Math.random() will never return 1 - it will return a number in the range [0,1). So I think you can safely multiply it by 10 rather than 9.99999.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 07-14-2003 06:22

Hi, Slime.
Thanks for making this thread.
I am dying to find a practice change to improve my JavaScript skill.
I am thinking now. But hasnot come anything yet.
Hmm......I will post again...
C U then.

Hiroki Kozai

Clay
Nervous Wreck (II) Inmate

From: Utreg, NL
Insane since: Nov 2002

posted posted 07-14-2003 17:39

On another board we had a discussion like this one a while ago. A way that came up (I can't take full credit for this one) is to just sort a predefined list, something like this:

code:
var range = [0,1,2,3,4,5,6,7,8,9];
range.sort(function(){ return Math.round(1- Math.random()*2)});

alert(range);


Although you'd be stuck with a pre-defined list of numbers.
How much that would actually matter would depend on the script that uses it.

peterned

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-14-2003 18:36

Alright the only question is whether this is correct probablity wise.
The first will always be larger than the second but I believe the probability is correct (well to be technically correct you can't programatically get a random number so even using the Math.Random function kills any true randomness but I'm going to assume we're overlooking that)

<script language="javascript">
first = Math.ceil(9*Math.random());
second = first - (Math.ceil(first * Math.random()));
alert(first +" "+ second);
</script>



.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 07-14-2003).]

MajorFracas
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2003

posted posted 07-15-2003 20:42

How about the following?

<script type="text/javascript">
num1=Math.floor(Math.random()*10);
num2=(Math.floor(Math.random()*9) + 1 + num1) % 10;
document.write(num1 + ", " + num2);
</script>



Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-19-2003 02:16

Yup, that works.

For those interested, the original solution that I had in mind was:

var first_number = Math.floor(Math.random() * 10);
var second_number = Math.floor(Math.random() * 9);
if (second_number >= first_number)
second_number++;

« BackwardsOnwards »

Show Forum Drop Down Menu