Copy c's rand() function

I’m trying to copy the rand() function from msvc in windows as I need to be able to use it for some code on Linux and would like to write the code in go. I found this stackoverflow question with the code and tried to mimic it below.

The issue is if I use the same seed of 36178 on windows I get these sequence of pseudo random numbers.

36178
19876
5709
27969

with the go code I only get the first random number the same and then they diverge and for the life of me I cant work out why. Assuming it’s something to do with the & 0x7fff bit in c really being undefined behaviour and go is doing something different.

36178
19876
32177
6811

Can anyone point me at what im doing wrong.

Hi @jeffsmith82
There is a small difference in your code:
See the Rand function:

func (r *RandomNumber) Rand() uint32 {
	r.Seed = r.Seed*214013 + 2531011
	return (r.Seed >> 16) & 0x7fff
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.