7

One way is to use an array of size n such that a[i]=i.

Now shuffle it.

Then use a loop to print the numbers. But can it be done without using the array?

Ps: Please do not use any inbuilt Random generator function. I am trying to get a logical answer and write that random function myself.

Shubham
  • 2,847
  • 4
  • 24
  • 37
Manish Kumar
  • 99
  • 1
  • 7
  • There are probably many similar questions here. Did you search for it? A (potential) answer is probably linked to [linear congruential generators (LCGs)](https://en.wikipedia.org/wiki/Linear_congruential_generator). Maybe start [here](https://stackoverflow.com/questions/44215505/random-number-generator-that-returns-only-one-number-each-time). – sascha Jun 11 '18 at 18:53
  • Depending on the range, you might be interested in a linear feedback shift register. Also, you can use [this technique](https://stackoverflow.com/a/50670084/56778). – Jim Mischel Jun 11 '18 at 20:18
  • See also [Xorshift](https://en.wikipedia.org/wiki/Xorshift) – Jim Mischel Jun 12 '18 at 22:04

2 Answers2

2

I'd say it depends on the quality of the random numbers you want to get.

An extreme case is to output the identity permutation and claim it's random. OK, they don't really look random at all.

A slightly more plausible solution is to take some prime p > n and some integer a > 1 which is a primitive root modulo p, and then look at a mod p, a^2 mod p, a^3 mod p, ..., a^{p-2} mod p. This will be a permutation of numbers 2, 3, ..., p - 1. Discard everything which is greater than n, and output the rest. (And insert the missing 1 at a random position somehow.) All this can be done in O(1) additional memory, by repeatedly doing x -> (x*a) mod p and discarding elements > n on the fly. And the result may be random enough for some applications: with a right choice of a, it will at least look pretty random to an untrained human eye. Still, not much more than that.

For obtaining quality random numbers, I doubt there is a general solution which uses O(1) additional memory. But don't take my word for it. Let's see what the other answers may bring.

Gassa
  • 8,546
  • 3
  • 29
  • 49
0

This answer is python based, but you can relate with any other programming language that you like.

import random # using random class
some_num = _ # any number
sample_num = _ # this should be less than or equal some_num
for num in random.sample(range(some_num), sample_num):
    print(num)

This generates random numbers and prints them using a loop.

  • 3
    Does `random.sample` claim to not use any additional memory? That's doubtful unless you can provide an explanation of how it does so. – Gassa Jun 11 '18 at 18:50
  • You are not defining any array previously. And we could use random.sample(range(1, some_num + 1), sample_num) instead. – loophole_sameer Jun 11 '18 at 18:55
  • Pretty sure that `range(some_num)` makes a list./array that has *some_num* elements in it. That would be storage or memory. – RBarryYoung Jun 11 '18 at 18:57
  • 1
    @loophole_sameer sorry i have not mentioned earlier that not to use any inbuild function. – Manish Kumar Jun 11 '18 at 19:23