Random and thread

Just starting out? Need help? Post your questions and find answers here.
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Random and thread

Post by Cyllceaux »

Hey there...

I want to generate 100000 Random numbers with different seeds.
example.

Code: Select all

For g=0 To 10
	RandomSeed(g)
	Debug g
	Define t=0
	For i = 0 To 100000
		t+Random(255)
	Next
	Debug t
	Debug "--------------------------------------"
Next
Important for me, that the random numbers for every start are the same.

Code: Select all

0
12827991
--------------------------------------
1
12737589
--------------------------------------
2
12758888
--------------------------------------
...

Now I hoped, I can create 10 Threads and run them parallel.

Code: Select all

Structure seed
	seed.i
	tt.i
EndStructure


Procedure rThread(*data.seed)	
	RandomSeed(*data\seed)
	For i=0 To 100000
		*data\tt+Random(255)
	Next
EndProcedure

NewList tt.seed()
For g=0 To 10
	Define *t.seed=AddElement(tt())
	*t\seed=g
	CreateThread(@rThread(),*t)
Next

Delay(1000)
ForEach tt()
	Debug tt()\seed
	Debug tt()\tt
	Debug "--------------------------------------"
Next

BUT: That do not work. It always use the last RandomSeed.

Code: Select all

0
12737656
--------------------------------------
1
12733613
--------------------------------------
2
12724172
--------------------------------------
...
How can I do this?
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Random and thread

Post by mk-soft »

Not testet, but have you activate compiler option thread safe
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Random and thread

Post by Cyllceaux »

stupid me... new Installation of PB... I forgot to set this setting... :oops:
XCoder
User
User
Posts: 68
Joined: Tue Dec 31, 2013 9:18 pm

Re: Random and thread

Post by XCoder »

It is easy to forget to set the compiler option thread safe. When I write a threaded program I always include the following code:

Code: Select all

CompilerIf Not #PB_Compiler_Thread
  CompilerError "Use Compiler-Option ThreadSafe!"
CompilerEndIf
This reminds me to set the compiler option thread safe.
Post Reply