PDA

View Full Version : Random Numbers



BobEdge
- 15th December 2008, 11:45
Hello,

I am trying to make a random numbrt generator that choses 5 numbers between 1 and 8. The problem i am having is that if the first number is 6 then the sequence will always be 61253, and if it is 1 then the sequence is always 12537, and so on for all 1 to 8. Any sugestions would be appreciated.

Here is my code:-

jmp1:
random temp
if sw1 = 1 then goto start
goto jmp1


start:
pause 2000
lcdout $fe, 1

number1:
random temp
if temp > 8 then number1
let num1 = temp
lcdout $fe, 1, dec num1

number2:
random temp
if temp > 8 then number2
if temp = num1 then number2
let num2 = temp
lcdout " ", dec num2

number3:
random temp
if temp > 8 then number3
if temp = num1 then number3
if temp = num2 then number3
let num3 = temp
lcdout " ", dec num3

number4:
random temp
if temp > 8 then number4
if temp = num1 then number4
if temp = num2 then number4
if temp = num3 then number4
let num4 = temp
lcdout " ", dec num4

number5:
random temp
if temp > 8 then number5
if temp = num1 then number5
if temp = num2 then number5
if temp = num3 then number5
if temp = num4 then number5
let num5 = temp
lcdout " ", dec num5

goto jmp1
end

Best regards
Bob...

skimask
- 15th December 2008, 11:58
I am trying to make a random numbrt generator that choses 5 numbers between 1 and 8. The problem i am having is that if the first number is 6 then the sequence will always be 61253, and if it is 1 then the sequence is always 12537, and so on for all 1 to 8. Any sugestions would be appreciated.
Random numbers with PBP aren't really random, they're pseudo-random.
If you seed the random number generator with X, you'll always get Y. That's just the way it is. That's what the PBP manual means by 'walking length'. All things being equal, the random number output will repeat itself with the same sequence if you use it exactly the same way every time.
Add some element of real random-ness, and you might get what you want...i.e. start up one of the timers and pre-seed the random value with the timer value when a button is pushed, that's just one example.

locko
- 16th December 2008, 16:35
I place a random statement to the main loop, that way while waiting for a button push you are randomizing in the background.

Paul

skimask
- 16th December 2008, 17:29
I place a random statement to the main loop, that way while waiting for a button push you are randomizing in the background.

Not bad...not a bad idea at all...pseudo-random - pseudo-randomness...

BobEdge
- 17th December 2008, 10:01
Thnx for the replies, as you can see i did have a loop like that right at the beginning of the program

jmp1:
random temp
if sw1 = 1 then goto start
goto jmp1

I first tried using a similar loop between each number fetching routine, but this meant pressing the button 5 times.

In the end i have used 2 pics. The first one uses 5 random vars to pause for random ammounts of micro seconds, and acts as the button press, the second one simply counts up until it sees a button press, then i use //9 to get the number from 0 to 8. checks to see if it is zero or has already been chosen & repeats if so.

In conclusion, the pbp random statement is the least random thing I have ever come across, a mate tells me he had a similar problem with visual basic, so it is not a flaw with PBP. I just wish the lottery used a computer, I might be able to work out the winning numbers.

Thanks for the help guys

peterdeco1
- 17th December 2008, 11:26
Try something like this:
LET X = 0

JMP1:
LET X = (X + 1)
IF X >=9 THEN LET X = 0
IF SW1 = 1 THEN START
GOTO JMP1

YOU WILL GET A RANDOM NUMBER FROM 0 TO 8. IT WILL BE TRULY RANDOM. IT IS POSSIBLE FOR THE NUMBER TO BE THE SAME WITH A SUBSEQUENT PUSH OF THE SWITCH.

mackrackit
- 17th December 2008, 11:39
Try something like this:
LET X = 0

JMP1:
LET X = (X + 1)
IF X >=9 THEN LET X = 0
IF SW1 = 1 THEN START
GOTO JMP1

YOU WILL GET A RANDOM NUMBER FROM 0 TO 8. IT WILL BE TRULY RANDOM. IT IS POSSIBLE FOR THE NUMBER TO BE THE SAME WITH A SUBSEQUENT PUSH OF THE SWITCH.

Not sure it can be called truly random. Given a certain amount of time the number will always be the same.

mackrackit
- 17th December 2008, 11:42
http://www.picbasic.co.uk/forum/showthread.php?p=57941

skimask
- 17th December 2008, 11:46
What I usually do when I need to use RANDOM, is to seed it with a 16 bit timer value, and then add a few other variables I've got around and always tie grabbing that random number with some sort of human interaction.
If you've got an event that relies on human interaction somehow (i.e. button press) and a tight loop somewhere, there ain't no way you're going to get a continuously repeatable result.
True, RANDOM in PBP isn't all that random, but when you start adding the 'YOU' factor to it, you can get decent results...

Ryan7777
- 17th December 2008, 12:18
Like maybe use a floating pin or some other means for a spare pin to pick up some "static" to generate an interupt or something?. But there again you may only pick up 60Hz (or 50), but i have heard of people doing this to get random randomness

ruijc
- 17th December 2008, 17:02
Like maybe use a floating pin or some other means for a spare pin to pick up some "static"

This has crossed my mind also ( didnt tryed yet though ).
I think that you can get decent random results using a spare pin with ADC grabbing nothing but noise.

You can then get 1 ou 2 bits from that "noise" which i believe it would be pretty random.

locko
- 18th December 2008, 05:55
This is untested, but was thinking of trying it someday....

The theory.
Leave an analog pin floating and take a voltage measurement to use as the seed for the random number.

Not sure how mach variation there will be in each of the measurements. The other thing would be to reduce the sample time for the conversion which may give to less accuracy and give better random results.
Also, if the pin is connected to a wire or long track, it should act as an antenna and increase the variation in the measurements.

Maybe some of the experts around here will have any ideas if this could work :)

Paul

eggman
- 18th December 2008, 08:47
I once did a project that used a DS18B20 temperature sensor. I used this sensor to also make the seed number for the random generator, worked quite well.