Generate a Random number between two variables

For my GCSE project, I am attempting to produce a complex random number generator, using Picaxe.

I wish to generate a number which must be above one number, but below another. Using the serout command, it will send this number to another picaxe.

This second picaxe will convert the number into two single digit figures, then convert them into binary. It shall then switch on the necessary output pins which will be connected to two 7-segment display drivers.

Despite the fact that I have not yet started to build this (I start next week), I cannot figgure out how I am going to be able to generate this number between two variables.

I ask for your help on this issue, plus your comments would be greatly appreciated.

Many thanks, Daniel Greaves.
 

Dippy

Moderator
There have been many postings, some recently, about random number generation.
First, have a search....
 
I can assure you that I have searched the forum, but my issue appears different to previous posts.

The fact that I wish to generate a number between variables is what makes it unique, and for me, challanging.

I applogise if there is a simple solution already on this forum, but please show me where, because I cannot find it!
 

Jeremy Leach

Senior Member
Hi Cosmic ...aka Daniel ;-)

I'd have a read-up in the manual about the Random command.

Plus ...the Random command generates a number in the range 0..65535. So you're saying you just want another range. Have a think ...
 

eclectic

Moderator
Daniel.

The "answers" are already in the Basic manual.(Accessed from Help in the programming editor).

Use the binocular icon to search for
random if then

That's all you really need to start.
e.
 

Michael 2727

Senior Member
You still need to generate a random number as per the method prevoiusly discusses.
When you have this random number it is just a matter how you apply it to the other
numbers you intend to use.
BTW are these other 2 numbers fixed or variable ?
 

hippy

Ex-Staff (retired)
I have found generating random numbers between two values hard in every programming language I have encountered so here are some details which should help.

RANDOM will give you a random number between 0 and 65535, but say you want just random numbers 1 to 6. One way to do this is by using the modulus operator, which obtains the remainder of a division ...<code><pre><font size=2 face='Courier'> result = number // 6 </font></pre></code> The remainder ( put into the result variable ) of any number divided by 6 will be 0, 1, 2, 3, 4 or 5 and you can convert that to 1 to 6 by simply adding one ...<code><pre><font size=2 face='Courier'> result = number // 6 + 1 </font></pre></code> But what if you wanted a number between 10 and 20 inclusive ? Well, there are eleven actual numbers there ( 10, 11, .. 19, 20 ) and we can create eleven numbers ( 0, 1, .. 9, 10 ) by using ....<code><pre><font size=2 face='Courier'> result = number // 11 </font></pre></code> And to get to the 10 to 20 values we can just add 10 ...<code><pre><font size=2 face='Courier'> result = number // 11 + 10 </font></pre></code> If we wanted a general purpose description without specific numbers we can see that the 10 is the minimum value, but what of that 11, how do we determine that from the only values we have, minimum and maximum ?

Well that 11 is actually 'maximum-minimum+1', 20-10+1=11.

Knowing that, we can say ...<code><pre><font size=2 face='Courier'> result = number // (maximum-minimum+1) + minimum </font></pre></code> will give a result which is always between the minimum and maximum inclusive.

Because the PICAXE can't use brackets, this has to be a two-step process ...<code><pre><font size=2 face='Courier'> temp = maximum - minimum + 1
result = number // temp + minimum </font></pre></code> This can be optimised to save having to create a 'temp' variable just for this purpose by using 'result' which gets updated in the second line to give ...<code><pre><font size=2 face='Courier'> result = maximum - minimum + 1
result = number // result + minimum </font></pre></code> From that it should be possible to work out what you need to do for your own purposes.

If you want to create a number which is above a minimum or below a maximum, we can do that by using the above but for 'minimum+1' and 'maximum-1' ...<code><pre><font size=2 face='Courier'> result = (maximum-1) - (minimum+1) + 1
result = number // result + (minimum+1) </font></pre></code> Which with those brackets dealt with leaves ...<code><pre><font size=2 face='Courier'> result = maximum - 1 - minimum - 1 + 1
result = number // result + minimum + 1 </font></pre></code> And, thus ...<code><pre><font size=2 face='Courier'> result = maximum - minimum - 1
result = number // result + minimum + 1 </font></pre></code>

Edited by - hippy on 06/09/2006 15:40:12
 

BeanieBots

Moderator
Not perfect but much easier.
Assume W0 holds random number between 0 and 65535.
w0=w0/10923 'it is now between 0 and 5 with a very slight bias.
w0=w0+1
W0 now holds random number between 1 and 6.
 

hippy

Ex-Staff (retired)
Well, they always say, &quot;there's more than one way to skin a cat&quot;, although no one ever gives any examples of how to at all :)

I have no idea, and not knocking your solution, but I'm wondering which solutions give more 'random' sequences than others ? I'm no expert in this field.

Neither of our solutions are particularly nice when simply using the next consecutive random number generated ...<code><pre><font size=2 face='Courier'>366665411112366125625435354426243626624242556143625
111111111111235366542411235365353542411236666653666 </font></pre></code> That's obviously down to the pseudo-randomness of the number generator in both cases. Not allowing the number to be the same as the previous improves things ...<code><pre><font size=2 face='Courier'>365412361256254353542624362624242561436256253613565
123536542412353653535424123653654235353541235412412 </font></pre></code> The moral is, that for 'true randomness', one has to define what that in itself means, and then tweak to get what one wants.
 

Mycroft2152

Senior Member
Nice coding guys. Now as educators, how would you grade Dan's GCSE project?

I'm sure he will share his &quot;A&quot; as someone else shared his LEDs.

I understand everyone wants to be helpful, but spoon-feeding the answer doesn't allow the student to learn the necessary problem solving skills.

I'm sure that after he graduates, his employers will be thrilled to know that his basic skills are to contact an internet group to solve his problems.

The PICAXE is primarily an educational tool. Rev-Ed did not create it specifically for hobbyists.

It is a fine line to walk. But keep in mind the educators are trying to do their jobs. the short term gratification of providing complete solutions should be weighed against the long term needs of the student.

Myc

 

Rickharris

Senior Member
To play devils advocate here a large majority of D&amp;T teachers wouldn't be able to derive the code Hippy supplied. (sad but true - even understanding it may test some).

The exam board are quite happy in general for some elements to be &quot;bought in&quot; as a black box provided suitable credit is given and the student declares the help (S)he got.

Even in the industrial world research includes asking &quot;expert&quot; opinion - that takes many forms - Credit is however expected to be given.

I think in this case the full response was justified.

NOW look what you started usless! <img src="smile.gif" width=15 height=15 align=middle>

Edited by - rickharris on 06/09/2006 22:23:57

Edited by - rickharris on 06/09/2006 22:24:20
 

Rickharris

Senior Member
Wahat would be wrong with testing the generated number and rejecting if outside range required - I realise this takes time because most numbers are outside the required range and produces long strings of numbers that are similiar. but that may not matter.

Neat solution hippy.
 

Mycroft2152

Senior Member
Rick, as the saying goes &quot;there's the rub&quot;. If credit is properly given, then the student's efforts and abilities can be fairly judged.

It's is true that is the real world experts are sometimes brought in for problems that are beyond the current skills of the team. But this is not the case, this is in a learnng environment. The teacher created a project for the student to learn. Whether or not the teacher could write the code himself is irrelevant. The purpose of the project may be the process to acheive the answer and the student is rated on the process as well as the results. It is not proper to disparage the teachers like that.

All that I am saying is that a slower step by step, give and take, approach is more beneficial the student's learning process and will allow him to grow.

I guess I'm just past the stage where being the &quot;fastest gun in the west&quot; (the person with the first, best, most complete answer) is important. I have to admit chuckling a bit when a see the posting that includes &quot;you beat me by a few minutes&quot;.

Myc

 

hippy

Ex-Staff (retired)
It's a fine line to walk, as has been said, and I did consider how best to answer before doing so. In this case there were a number of reasons for giving a detailed explanation -

This is just a small part of the project as a whole. I don't think the GCSE or his future employment prospects rest or fall on this one part.

I am not convinced that an answer to the problem is so obvious by reading the PICAXE manuals alone.

On top of that, if Daniel is really determined to search for an answer, he'd eventually find it on my web site and a number of others anyway.

I agree that students need to learn how to learn, but on the other hand, if helping them over one hurdle helps them solve the rest they may well learn more than otherwise. The way I saw this was that if building a house was the project, explaining how to mix cement properly gives no major advantage.

On the issue of employers finding their staff are getting their answers from the web or by simply asking others, many would call that initiative, and I suspect few these days really care as long as the job gets done.
 

hippy

Ex-Staff (retired)
<i>It's is true that is the real world experts are sometimes brought in for problems that are beyond the current skills of the team. But this is not the case, this is in a learnng environment. </i>

Perhaps the question is, what is the student learning or meant to be learning, and at the end of the day what are they expected to have demonstrated they can do or have learned ? What is being assessed ?

As budding programmers, electronics or software engineers, are they expected to ponder over some complex mathematical intricacy or are they learning how to put together code in a logical and functional design which does what it is meant to ?

If the challenge of the GCSE project is to generate a random number within contraints then I will accept that I may well have over-stepped the mark ( although there is still a lot more to randomness which isn't resolved by what I gave ), but, while fundamental to the project, I don't see it as a great part of the whole.

I'm not after arguing I'm right or wrong. We can all only make the best call as we see it, and I have no problem when people point out that the wrong call may have been made.
 

Dippy

Moderator
This really has opened a very important can of worms.

One question: Now you've done part of his homework for him, are going to do the full code for his serial link too?

And the analogy 'fastest gun in the west' is just about right. It seems to be a competition as to who is the fastest/smartest.

Perhaps its common knowledge in Schools now that if you want your school project completed for you then just get on this Forum. Yes, I fully agree, finished examples can be a great way to learn. Thats how I learnt SCL and S3 (anyone remember it? Cos I don't). But providing a nearly-finished answer to a specific school exam question is a bit OTT yes/no?

And just look at the detail of code in VB given to Useless in his threads. Its amazing the amount of work others will go to. People spent ages looking, coding, tidying etc. knowing Useless seems to put in no effort himself. WHY? (and Useless probably went down the Gym. to pump the pecs.)

After the Useless lesson, here we go again.

Edited by - Dippy on 07/09/2006 10:18:20
 

hippy

Ex-Staff (retired)
<i>This really has opened a very important can of worms </i>

Absolutely, but I don't think there's any simple answers to be had.

<i>One question: Now you've done part of his homework for him, are going to do the full code for his serial link too? </i>

What help I'd give would depend upon exactly what was being asked for, and what I thought would help the person best. I will admit that I am generally disposed to presume that someone asking for help is doing so because, while capable, they ( for whatever reason ) cannot see the solution and are banging their head on the wall, and to say, &quot;Open your eyes and look harder&quot;, will not help them at all.

<i>And the analogy 'fastest gun in the west' is just about right. It seems to be a competition as to who is the fastest/smartest. </i>

I don't agree, and that's not what motivates me. Maybe others see it differently. The way I see it is that people want help and if I'm able to I will give the best help I can.

<i>Perhaps its common knowledge in Schools now that if you want your school project completed for you then just get on this Forum. </i>

But how many times has that happened ? I don't think many at all, and if people are asking for help I believe we are all here primarily to help and it is not our job to police the education system or a student's learning experience.

And who are we to know or judge what is best for any particular student ? As I've said, I believe those who 'cheat' by coming to take an answer are only fooling themselves in the long term, and if they are fooling the education or examination system as well, then the problem lies there not here.

On the other side of the coin, we have had the 'OMG I have to finish this tomorrow and haven't started' posts which have received short shrift.

<i>But providing a nearly-finished answer to a specific school exam question is a bit OTT yes/no? </i>

In this case, while I gave a specific answer, I don't believe that is a significant part of the project as a whole. I would almost certainly have responded differently if it were known to be a specific exam question, but then the student could have simply neglected to mention such a fact and we'd be none the wiser. If we ask if it is, a student could likely say &quot;no&quot;, even if it were. Do we start to take all questions as exam questions even though not stated as such, and simply tell posters, everything they need to know is in the manuals or can be learned through study ?

One could genuinely say we are all doing everyone a disservice by giving any answers at all if the information already exists elsewhere or can be determined.

Perhaps the only way to judge if what answers are given are reasonable or go too far is to compare them to what an in-class lecturer or mentor would provide if asked directly. Not being one I can't say. The best we can hope for is one of those to say when we've got it wrong retrospectively.

That then leads to a further question; are we here as &quot;educators&quot; or &quot;founts of knowledge&quot;, or both ?

<i>And just look at the detail of code in VB given to Useless in his threads. Its amazing the amount of work others will go to. People spent ages looking, coding, tidying etc. knowing Useless seems to put in no effort himself. WHY? </i>

I don't think usless gets anything more than anyone else when there are people who can answer a question or give advice. In my case the amount and detail of response does depend upon whether I know the answer ( and although complex it can be easy to provide ), or how well the question motivates me to find a good answer myself.

The answers given, while in response to questions, are also there to help a wider audience, even beyond the PICAXE community.

The bottom line is, that in my perfect world there would be a book which had all the answers in for people who want them and we couldn't stop students or anyone from looking in the book to get what they are given here. The only difference is that we are writing the book as we go.

One final point. If someone we know to be emminently capable but unable to solve the problem had asked the same, would we have given the answer and explained it them ? I expect so. That solution would then have been documented for anyone to find no matter what their motive or intent, so is there really any difference in having provided the answer in the past to doing so now ? We're firmly into philosophy there, which is what makes it so hard to give any straight-forward and simple answers on the whole subject.

Edited by - hippy on 07/09/2006 14:33:10
 

Michael 2727

Senior Member
Slightyl OT It's not about being the fastest
gun in the west at all.
You read a post, decide you may have some useful input to ad to the subject.
You take the time to write something, the phone rings, then you try to locate the &quot;can&quot; to get the correct information off
the label (manufacturer) then do a quick search to see if the product still exists.
Then when you post the reply 40 mins has passed and 7 other people have already answered, the first 3 have said exactly what
you have just posted.
It can appear as if you were not paying attention, when you actually were.
That's all there is to it.
 

hippy

Ex-Staff (retired)
Maybe it's because of the swiftness of responses that such an impression also arises ?

I'm fortunate that I do have plenty of time on my hands ( and at odd hours ) and can answer more promptly and more frequently than others, and do give more time to this forum than others do or can afford to. And I can't deny that I do get enjoyment and satisfaction out of being able to give people the help which they are seeking.
 

whizzer

Senior Member
It&#8217;s the internet that makes that brings these issues about of course, &amp; for previously generations of educators, this issue wouldn&#8217;t have even come up.

Daniel was open &amp; honest in stating up-front that his question related to a GCSE project, but are others going to be so honest and declare that it&#8217;s a school project when asking their questions? Probably many won&#8217;t. And how can this be policed anyway? Kids can be brilliant at manipulating their parents to achieve what they want -and so for many it&#8217;s probably just a small step to take these skills to the web.

We&#8217;ve already got &#8216;distributed processing&#8217; in computers &#8211;maybe we will just have get used to it &amp; call this &#8216;distributed researching&#8217; and accept this as part of the new &#8216;world order&#8217;. Sounds radical I know, but is there any alternative?
 

Rickharris

Senior Member
The exam boards encourage students to research unknown areas in any way possible.

They even (in the case of product design anyway) allow students to &quot;buy in&quot; black box solution to parts of the problem that are complex. the student is expected to specify what the &quot;black box&quot; does - the functionality - but may not understand how it works. As long as this is not the Entire project <img src="smile.gif" width=15 height=15 align=middle> it is acceptable because the student is creating a product, the end product is the target getting there is a matter of knowing what you need (perhaps not of being able to self make that part) .

We not longer make Screw threads, Most fixings and fittings, Finishes and the glue we use are all bought in. I go back to my reply in the LED competition thread. I think we as a group of enthusiasts have to assist - encourage and help the less able users who come here hoping for some enlightenment. Sometimes we are giving away assistance at the expense of experience - but hey we don't get paid and we ain't in a programmers magic circle selling the trade secrets are we. The education world will have to learn to accommodate the new information age and come to terms with the implications - in most cases I guess teachers are just pleased the student is showing some interest and making progress.

Many employers would be pleased they have an employee who has the intelligence, drive and knowledge to ask someone who can give a correct answer - hopefully as they implement that answer they will also gain understanding.

Modern times - How well I recall melting Animal bone/horn and hoof glue in a glue pot over a gas ring to glue together my carefully crafted bookcase.

Edited by - rickharris on 07/09/2006 16:57:18
 

Mycroft2152

Senior Member
Poor Dan walked into the middle of a firestorm. He was honest and upfront about his project too. Sorry about that Dan.

It's has been an interesting thread, but I don't beleive this discussion will fundamentally change the way responses are given on this forum. There may be a slight shift, but it is only temporary.

Human nature is fascinating, people will ALWAYS be able to provide a rationale and excuses for their actions, even Hitler or Napoleon. Whether it is right or wrong is only the opinion of others.

My purpose in adding to this conversation, was to ask posters to consider the effects of their rationalized &quot;fastest gun in the west&quot; responses to the educational process.

Teachers have enough problems doing their jobs without having others add to their problems by providing internet based answers.

Technology has provided today's student many ways to find answers by cheating. If it were only the results that counted, then the no cell phone rules and the requirement for calculators without memory. etc., during tests wouldn't be needed.

There is a vast difference between the learning experience of study group, and getting correct answers off the internet.
The payment for these internet answers is usually money, but sometimes it is simply based on prestige and ego. Would some of the responses have been the same if the requests came in with the offer of X dollars, pounds or euros from a student? I could see the bidding war; &quot;I can do it fo 10&quot;, &quot;No, I'll do it for 7&quot;.

It is everyone's responsibility, teachers, parents and mentors to provide the best educational experience for the students.

Something to think about.

Myc


 

BeanieBots

Moderator
I think we have completely lost the plot here!
I'm not suggesting that what has been said is not valid, simply that what initiated it was way off mark.
If the solution to creating a scaled value constitutes a GCSE project, then we really are in trouble!
Please take a good look at the majority of answers on this forum. The majority point students in the right direction. In particular, when it is obvious that a student is simply fishing for a quick answer, the help is even further restricted.
 
I appologise for the delay in my reply, and also the debate that I have initiated, but I can assure you that my coursework is far more complex than I need, but have been asked to decide on a project that will challenge me. For several years, I have been a hobbyist of picaxe, and have learnt more about it than my teacher knows. (He asks me for help)

I would have been able to figure out a solution for myself, but due to the limiting factor of time, I asked here in the hope that I would gain advice. I was delighted in the detail of my reply, and appreciate how far people went for me.

I am confident of being able to solve all other problems myself, but hope that this debate initiated will not comprimise the help some people are willing to give.
 

adub

New Member
One of the real issues in this discussion is if &quot;useless&quot; has actually learned anything from the answers posted on this forum.

I say yes. Look back at some of his earlier questions and compare to his later ones and you can see he has learned a lot.

His questions have sparked many interesting responses that I have also learned from. Hope he doesn't just fade into the night. I for one have enjoyed his enthusiasm.

He does remind me a lot of a certain Jeremy's entertaining posts.

Let's just &quot;Get on with it!&quot; and have fun while we are doing it!

Retired educator:
Arvin
 

hippy

Ex-Staff (retired)
<i>I am confident of being able to solve all other problems myself, but hope that this debate initiated will not comprimise the help some people are willing to give. </i>

I am sure it won't and, as it was earlier put, you perhaps stepped into a firestorm not of your making. No matter what that help is or how it is provided, everyone can I think be assured that there are people here to help.

That also goes for 'getting better' ( ne 'usless' ). Things happen, lessons are learned, we can all move on.

And I'm sure this debate will continue or be revisited in the future. That's what happens within thriving communities.
 
Top