-1

I'm looking for an assembly function that generate random number from 1 to n. n value could be about 60000. I have no idea how to do that. I've searched for this and i couldn't find any that met my excpectations.

Jester
  • 56,577
  • 4
  • 81
  • 125
sheddar
  • 270
  • 1
  • 4
  • 16
  • For what CPU? ARM, Motorola, Intel all have very different assembly instructions. – Eric J. Feb 06 '15 at 18:36
  • What platform/assembler? – Alex K. Feb 06 '15 at 18:37
  • @ Erick J. Intel @Alex K. MASM x86 – sheddar Feb 06 '15 at 18:56
  • the same way you would in any other language...what problem are you having? – old_timer Feb 06 '15 at 18:58
  • 1
    Are you looking for `RDRAND`? – Cory Nelson Feb 06 '15 at 19:18
  • @Cory Nelson i've tried to use it but i have this error: _Error 2 error A2085: instruction or register not accepted in current CPU mode_ – sheddar Feb 06 '15 at 19:40
  • @kobasek RdRand wikipedia page says: RdRand is an instruction for returning random numbers from an Intel on-chip hardware random number generator.RdRand is available in Ivy Bridge processors ( In some Ivy Bridge versions, due to a bug, the RdRand instruction causes an Illegal Instruction exception). so it better to use Linear Congruent Generator. – Parham Alvani Feb 06 '15 at 20:03
  • I've change .586 to .686 and it started to work. I mean it began to compile. Now when i'm trying to use it i got this: _An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in GUI.exe_ – sheddar Feb 06 '15 at 20:37
  • please add your code. see [this](https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.sehexception%28v=vs.80%29.aspx) for documentation about your error – Parham Alvani Feb 06 '15 at 20:47
  • Possible duplicate of [Randomizing Numbers in Assembly with MASM32](https://stackoverflow.com/q/16170871), but that doesn't ask about limiting the range to 1..n – Peter Cordes Sep 02 '18 at 19:34

1 Answers1

3

Linear Congruent Generator:

r[n+1] = (a * r[n] + c) % m

m = 65537 (216+1)

a = 65538 (a - 1 must be multiple of any prime divisor of m, and single divisor is m itself, i.e. 65537)

c = any even number (c and m must be relatively prime)

[Knuth, II vol., 3.2.1.1--3.2.1.2]

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
  • I want to use this algorythm in Miller–Rabin primality test. But, im passing to this test a variable which is another generated number. This variable is my higher range(0 - variable-1) How can i adjust this algorythm to this? – sheddar Feb 17 '15 at 19:10
  • You can implement any LCG with large module, and then take the remainder when dividing by the `range`. See http://en.wikipedia.org/wiki/Linear_congruential_generator, where you'll find common used values of `a`, `c`, and `m`. You can use `m` that equal to power of 2 to simplify your code (and use higher bits of result, if you need more random generator). After all just `div` a random number by your range, and get remainder from `dx`/`edx` register. – Mark Shevchenko Feb 17 '15 at 20:09
  • Thanks, it works but after every compiling i get the same sequence of generated numbers. It's normal? – sheddar Feb 18 '15 at 19:50
  • The sequence depends on initial value (r[0]). The exact initial value allows you to debug a program, cause the sequence is the same always. But in release version you should initiate the sequence by pseudo-random number, f.e. by result of the [`RDTSC`](https://en.wikipedia.org/wiki/Time_Stamp_Counter). – Mark Shevchenko Feb 19 '15 at 08:20