PDA

View Full Version : 6 random numbers limiting to 49 ?????



SuB-ZeRo
- 14th June 2005, 23:23
hi i want to generate 6 numbers between 1 - 49 (1 and 49 include) here is my codes but always give me 0 to all numbers and i don't want 6 possibles the same [i think i will use if and then commands really much :)]
i am useing 16f628 in 4 MHz and LCD=16x2
'''''''''''''''''the codes'''''''''''''''''''''''
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
pause 200
sayi1 var word
sayi2 var word
sayi3 var word
sayi4 var word
sayi5 var word
sayi6 var word
b1 var byte
b1=0
tus_bekle:
button porta.4 ,0,0,0,b1,1,tus_geldi
goto tus_bekle
tus_geldi
random sayi1
random sayi2
random sayi3
random sayi4
random sayi5
random sayi6
sayi1 =sayi1 & $31
sayi2 =sayi2 & $31
sayi3 =sayi3 & $31
sayi4 =sayi4 & $31
sayi5 =sayi5 & $31
sayi6 =sayi6 & $31
lcdout $fe,1
lcdout "NUMARALARINIZ"
LCDOut $Fe,$C0
LCDOUT #sayi1,":",#sayi2,":",#sayi3,":",#sayi4,":",#sayi5,":",#sayi6
end
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''

Darrel Taylor
- 15th June 2005, 06:57
Hi SuB-ZeRo,

The RANDOM function requires a "Seed" to be placed in the variable before using it.

For instance,

sayi1 = 12345
RANDOM sayi1

Unfortunately, doing it that way, you will end up with the exact same numbers every time you run the program. One way around it might be to let a Timer free-run untill some external event happens, such as a button press, or maybe a character being received via RS232. Then once that happens, use the timer value as the seed.

Once you have the seed, you'll need to use the same variable for each of your 6 random numbers, then copy it to the other variables as needed.

As you start playing with the RANDOM function, you'll realize that it's not very Random at all. If you keep getting random numbers from the same seed, you'll start seeing a repeating pattern. And if the seed is set to 0, you will only get 0's as the random number.

Keep in mind that the number will be between 1 and 65535 so if you want to limit it to a certain range, you'll have to do a little math on it.

HTH,
   Darrel

SuB-ZeRo
- 15th June 2005, 18:54
I really dont understand if random is not a random then what does it means?
Re u try to say :U can not find random numbers with random comman.If i can't find random numbers with random command , how can i find random numbers?Then what the random command do?I really don't understan :(

Dwayne
- 15th June 2005, 19:34
Hello Sub-zero,

SZ>>I really dont understand if random is not a random then what does it means?
Re u try to say :U can not find random numbers with random comman.If i can't find random numbers with random command , how can i find random numbers?Then what the random command do?I really don't understan <<

Ok, a computer can *never* really pick a random number... But there are certain "algorithems"<sp> that give a "apparent" random number. Lets take a example ok???

Lets Look at PI. PI does not have any "repeating" sequences. Thus, we can say the numbers look random. Every time we turn on our computer and say Pick a Random number, it will pick (lets say) the first digit. The next time we call "Random" it will pick the following digit... and so forth... When we turn off our computer and turn it back on, it will start the process all over again... starting with the first digit...second digit... third digit...etc...

But what happens if you do not want this to happen *exactly* the same, every time you turn on your computer??? You put in a "SEED". A very good seed is the "time".

digit = time (in seconds) * the Lookup number.

Thus if time = 5 the first digit is 5 *1 = 5 which means it will look at the 5th digit of PI. The second time it looks it will look at digit #6 etc etc.


Thus if time = 10 the first digit is 10 *1 = 10 which means it will look at the 10th digit of PI. the second time it looks, it will look at digit #11. etc etc.

Thus if time = 160 the first digit is 160 *1 = 160 which means it will look at the 160th digit of PI. the second time it looks, it will look at digit #11. etc etc.


If you notice, the SEED "Time" is *always* varying to *some* degree...Thus when we turn on our computer, the chances that the seed will be the same as before is very slim.


This is a crude example, but I hope it helps..

Dwayne

Darrel Taylor
- 15th June 2005, 19:43
It's called a "Pseudo-Random" number generator.

It's main use is probably for creating "White Noise" with the SOUND command. Well, truthfully, it creates "Pink Noise", since "White Noise" would require a truly Random number generator.

I wouldn't want to write a game based on these numbers, because it would become too predictable.

One possibility for generating better randomness, is to take the amplified noise output from a semiconductor such as diode or transistor (White Noise) and feed that into a timer or A/D Channel. When you use those values, you'll never see a repeating pattern. But then, that means more hardware.

I see Dwayne beat me to the punch. And, I'll agree. Time would make a good seed, as long as it's a battery backed-up clock that still runs when power is off.

HTH,
&nbsp;&nbsp;&nbsp;Darrel

mister_e
- 15th June 2005, 23:27
All of that sounds familiar.. have a look to this thread
http://www.picbasic.co.uk/forum/showthread.php?t=849&highlight=bartman+random