random numbers
bluejay
Posts: 131
in BASIC Stamp
I am trying to write a program to randomize numbers from 1 to 70 without success. Does someone have such a program? Thanks
Comments
I see you also asked this question about the Propeller 1. The same logic applies for the Stamp, except the Stamp will use a 16-bit word.
That will play out the same series of pseudorandom numbers each time you start the program. For variation, seed the value of flip at the start.
Is that what you want by "randomize numbers from 1 to 70"? That could also mean you want all the numbers 1 to 70 inclusive, in a random order.
Syntax error: I'd written flip=RANDOM flip, but it should be simply RANDOM flip. Modified accordingly in my previous post.
If you do want a table of values from 1 to 70 in random order, we first have to know the destination. There is enough scratchpad RAM in the BS2P series. Or eeprom in the other Stamps, so long as the values don't change often? Or some external memory or device?
It' a start. But I am trying to generate 6 non-repetitive two decimal random numbers from 1 to 52, so I can play Lotto.
I haven't written a BS2 program in a while, so I tracked down a board and gave it a go. The key with randomizing the pseudo-random RANDOM is to get a human involved. If you constantly shake the number until a button is pressed, you'll get random results.
Thanks jonnyMac for the program. It works fine. The set of numbers is displayed in set of twos for a total of 6 indefinitely. How to make the program display any set of 6 numbers for a total of example 10 sets? Thanks
I used DEC2 to keep the screen nicely formatted; there are only six numbers per "draw."
Just add another counter variable and stop when it hits 10. Maybe I don't understand your question. Why don't you show an example of the outcome you'd like to see on screen. As I say to my clients, the root word of specification is specific.
The outcome I would like to see on the screen is 6 numbers per draw for a total of 10 sets or any other amount of sets.
When I add a variable in the program the problem is that the sets of numbers instead of displaying a set of 6 numbers across the screen, it would display the sets across the entire screen horizontal or vertical.
Could you please show me where I can add the variable in the program so that it stops at 10 sets? Thanks
All BS2 variables are global and they're defined together. You can use a Nib since you're only going to 10.
When I added the variable "m" and then running the program the result is showing 10 sets of numbers. The problem is that the numbers are not random.
Show_Draws:
FOR m = 1 TO 10 ' display
FOR j = 0 TO 5
DEBUG DEC2 draws(j), " "
NEXT
DEBUG CR
NEXT
STOP
PAUSE 250
GOTO Main
bluejay,
The problem with your block of code is that your "m" has no relation to your Draws; it's just printing the same array over and over again.
FOR m = 1 TO 10 ' display
..FOR j = 0 TO 5
....RANDOM lotto ' stir again
....ball = (lotto // 52) + 1 ' get ball from 1..52
....DEBUG DEC2 ball, " " ' Changed from Draws(J) to ball
..NEXT
..DEBUG CR ' Go to the next line
NEXT
Argh - Hate this new system!
Well, yeah -- you're not calling the code that randomizes the draw; you're simply reprinting the same data set over and over.
I don't know if this is what you want because you refused my simple request to draw out what you want to see on screen.
FWIW, for the best randomness, the user should have to press the button for every "ball" -- just like in the real lotto.
The program is working better, however each time I run it all results show same numbers. I would like to see new numbers for each tokenize please.
Please read about pseudo-random number generators (hint: they're not truly random). As I have explained twice, the only way to get random results from a BASIC Stamp is to involve some external random stimulus (e.g., a human pressing a button).
Ok JonnyMac, I will use a button to generate random numbers. Thanks
Remember... to keep things random you must stir the PRNG until the button press event. If you simply call RANDOM with each button press, you will get the same sequence from the same seed. BTW, this is how Simon games operate: When the game is started, a seed is created that lets the game re-play the pseudo-random sequence from the PRNG. One of the first demos of the original BASIC Stamp 1 was Simon.
Here's how you can stir the PRNG while waiting on a button
When this code falls through you have random number in lotto. Copy that to another variable for work, because lotto is the PRNG seed. You can see all of this in my first example.
If for some reason you want to avoid pressing a button, there are alternatives that use the RCTIME command with an external circuit to provide a few random bits to break the pseudoRANDOM sequence. The external circuit can use something like a light or temperature sensor to add the uncertainty, or even simply a loop of wire with AC/EMI pickup can stir the low bits of the RCTIME result.
I will take your suggestion. I know that generating random numbers is done using MS Excel without using external stimulus hardware. It's in you tube.
Maybe somebody out there can come up with an algorithm in BASIC language to generate random numbers through software only. One can only hope.
bluejay, you're comparing the resources available to the Stamp vs. a laptop. They're not the same, the laptop is much more complex.
A lot has been written about the random number generator in Excel, but where it gets its seed has not been disclosed. It's not all happening in software, Excel pulls the seed from somewhere in the computer. The code to generate PRNGs in Excel has been updated several times, and people have attempted to find out from Microsoft where the seed comes from. The usual answer has been that it pulls the seed from "the system time" when the spreadsheet is recalculated. But there are a few ways to access the system time in a computer, so this is vague.
My point is that the methods suggested above are what you have to do to get a good random number on a microprocessor like the Stamp. And they work!
I will work on it.
How about using the system clock for the seed?
it will repeat the same numbers every time you reset, since the number of instructions is the same.
You need some randomness in the seed to have different numbers, therefore the suggestion to wait for a initial button press and THEN use the clock as seed.
Enjoy!
Mike