random number in Edata - Page 2

# Thread: random number in Edata – 60 days old

1. 1

## Re: random number in Edata

I was re-reading my post and this sounded very negative (I am dutch but that was probably already clear). Anybody must be (of course) everybody...

Originally Posted by amateurtje

Thanks for anybody's reaction and helping me. It actually does something really small in the program but cost me about half a day or more...

thanks all

2. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

3. 0

## Re: random number in Edata

This is the classic generator used by many languages, since the times of Fortran and Pascal.

It generates 32-bit random numbers and turns them into Float numbers in the range from 0 to 0.99999999.

If all you need is integer numbers, you could eliminate the Float part and save some memory.

An additional option used in many languages is to initialize the "seed" with a value taken from a timer or ADC so that each time the sequence starts from a different number. If you need, you could do it easily by initializing the RndInt variable on power-up with the value taken from an ADC.

Before to post the code I have adapted it to Proton Basic, and tested on a 12F1572 with compiler version 3.7.1.8

Code:
' =================================================
'  RANDOM GENERATOR
' =================================================
Dim RndInt As Dword
Dim RndFloat As Float

Sub Update_Rnd()
' --------------------------------- generate a new Int32 random number
If RndInt = 0 Then RndInt = 1
If (RndInt & 0x800000) <> 0 Then
RndInt = (RndInt << 1) ^ 0x1D872B41
Else
RndInt = RndInt << 1
End If
' --------------------------------- convert the new RndInt to RndFloat
RndFloat = RndInt
RndFloat = RndFloat * 0.0000000002328306437
EndSub

' =================================================
'  Main Loop (used to test the random numbers)
' =================================================
Main:
Do
Update_Rnd
HRSOutLn Dec RndInt, "  ", Dec8 RndFloat
HRSOutLn ""
DelayMS 1000
Loop

4. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

5. 0

## Re: random number in Edata

Therre is another simpler way, but it will require a program written on teh PC that adds a random number to the HEX file before it is programmed.

Thie could be a PC app that reads the HEX file, alters it with the random numbers, saves it back, overwriting the original, then calls the programmer. The eeprom data in a HEX file is very easy to find, but it will also need to have its CRC value altered based upon the random number placed in the HEX file.

This would make the whole process invisible to a user.

6. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

7. 0

## Re: random number in Edata

My previous post is pretty useless, I just found out that the Proton already contains the Random function.
And what I wrote does the same functions as the Proton Random.

Reading this code can only be useful to understand how random numbers are made.

8. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts