I have to write a code for generating random numbers without using random module and its function.
How do I do it? I tried using the system time as it keeps on changing (importing milli seconds) but with tht I am able to get random nos in the range of thousand only.
sanchitgarg
- 4 Contributors
- forum17 Replies
- 18 Views
- 8 Years Discussion Span
- comment Latest Post by sanchitgarg
vishesh 59
I have to write a code for generating random numbers without using random module and its function.
How do I do it? I tried using the system time as it keeps on changing (importing milli seconds) but with tht I am able to get random nos in the range of thousand only.
Well there are several algorithms to generate random number, simplest to my knowledge being Linear congruential generator(LCG).
The general formula for LCG is X_{n+1} = \left( a X_n + c \right)~~\bmod~~m . The recommended value for a, m, c are given in Wikipedia page. It is recommended use those combinations of a, m, c for more randomness. Start with any value of X and for different random numbers everytime you run code choose X using system time.
To get number in particular range, eg [L, H) apply this to every random number generated(say Y) : Z = L + Y%(H-L)
Edited
by vishesh: n/a
vegaseat 1,720
A time based random number generator will only work if called once, or uses the random time between a user input event.
The function time.time() only updates 18.2 times per second, too slow if you request several random numbers quickly.
Edited
by vegaseat: n/a
sanchitgarg
A time based random number generator will only work if called once, or uses the random time between a user input event.
The function time.time() only updates 18.2 times per second, too slow if you request several random numbers quickly.
yeah i am facing many problems using time. also it generates a sequence of nos in a ascending order.
i have written a code for nos between 1 to 100. here it is:
import datetime
from time import sleep
l=[]
tym=[]
n=int(raw_input("Enter the no. of numbers to be generated"))
for i in range (n):
time=datetime.datetime.now()
t=time.microsecond
l.append(t)
sleep(0.012)
print l
for i in (l):
tym.append(i/10000)
print tym
tats why I asked for some other logic which does not have a limited range.
sanchitgarg
Well there are several algorithms to generate random number, simplest to my knowledge being Linear congruential generator(LCG).
The general formula for LCG is X_{n+1} = \left( a X_n + c \right)~~\bmod~~m . The recommended value for a, m, c are given in Wikipedia page. It is recommended use those combinations of a, m, c for more randomness. Start with any value of X and for different random numbers everytime you run code choose X using system time.
To get number in particular range, eg [L, H) apply this to every random number generated(say Y) :
Z = L + Y%(H-L)
I tried using this formula. It works fine for small nos (not exactly small but upto 1000000000) After that it starts giving L at the end of the no. How do I increase its range?
vishesh 59
You mean output sort of this 132434454545435435L
.
Well this happens when the number gets greater than usual integer type range. Python converts normal integer into Long type and Python shows L after long types. When you do print num
, L doesnt shows up.
Edited
by vishesh: n/a
sanchitgarg
You mean output sort of this
132434454545435435L
.Well this happens when the number gets greater than usual integer type range. Python converts normal integer into Long type and Python shows L after long types. When you do
print num
, L doesnt shows up.
print num means? Could you please tell the syntax. I want to print a list of nos.
vishesh 59
What I mean is instead of directly printing whole list, you have to print numbers individually, to avoid L at end:
for x in rand_num:
print x
sanchitgarg
Is there any other method or logic except that formula?
ov3rcl0ck 25
Well there are several algorithms to generate random number, simplest to my knowledge being Linear congruential generator(LCG).
The general formula for LCG is X_{n+1} = \left( a X_n + c \right)~~\bmod~~m . The recommended value for a, m, c are given in Wikipedia page. It is recommended use those combinations of a, m, c for more randomness. Start with any value of X and for different random numbers everytime you run code choose X using system time.
To get number in particular range, eg [L, H) apply this to every random number generated(say Y) :
Z = L + Y%(H-L)
Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).
sanchitgarg
Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).
Right now i have used time for my input. Could you please give a bit more info on used memory and mouse movement?
vishesh 59
Is there any other method or logic except that formula?
There are of course others out there. But this one is the simplest and produce decent set. You can search for pseudorandom number generator algos.
http://en.wikipedia.org/wiki/List_of_algorithms
>> Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).
These all will help you to seed......but to generate sets you need some method!
sanchitgarg
There are of course others out there. But this one is the simplest and produce decent set. You can search for pseudorandom number generator algos.
http://en.wikipedia.org/wiki/List_of_algorithms
>> Probably the best inputs is date/time, used memory, random user input, and my favorite mouse movement(truecrypt uses this).
These all will help you to seed......but to generate sets you need some method!
I have searched wikipedia n saw many other algos. Rest are too compicated.
Wats the meaning of seed?
n sets refers to the list of random nos?
vegaseat 1,720
This Python code shows you how to pick a random number from a list ...
# picking random numbers from a list
# Python2 syntax
def pick_rand(rand_list, start=-1, incr=1):
"""
closure based function that cycles through rand_list one index
at a time for each call, index resets to zero at end of list
"""
def inner():
inner.index += incr
if inner.index >= len(rand_list):
inner.index = 0
return rand_list[inner.index]
inner.index = start
return inner
# random number list of 100 numbers from 0 to 99
# created by timing user input events
rand_list = [
6, 40, 3, 6, 93, 81, 84, 56, 43, 31, 34, 21, 9, 96, 84, 87, 6, 9,
12, 0, 87, 59, 31, 18, 6, 9, 96, 68, 56, 59, 31, 3, 75, 62, 34, 21,
9, 96, 68, 56, 43, 0, 71, 59, 31, 34, 6, 78, 65, 37, 25, 96, 84, 71,
59, 46, 34, 6, 9, 81, 53, 40, 28, 84, 56, 43, 65, 84, 87, 59, 62,
50, 21, 9, 96, 84, 71, 59, 46, 18, 90, 93, 81, 84, 71, 59, 31, 34,
53, 40, 46, 3, 75, 75, 50, 6, 65, 21, 87, 15
]
# testing only ...
print len(rand_list) # 100
print "average = %d" % (sum(rand_list)//len(rand_list)) # 48
# needed!
rand = pick_rand(rand_list)
# show 10 random numbers
for k in range(10):
print rand(),
print
# show another 10 random numbers
for k in range(10):
print rand(),
The list is generated this way ...
# a time based random number generator
# that uses the random time between a user's input events
# to create a list of random numbers
# Python2 syntax
import time
def random_number(low, high):
"""
a time based random number generator
uses the random time between a user's input events
returns an integer between low and high-1
"""
return int(low + int(time.time()*1000) % (high - low))
low = 0
high = 100
count = 0
rand_list = []
print "Press enter for a random number (q to quit):"
while True:
sel = raw_input()
if sel == 'q':
break
rand = random_number(low, high)
rand_list.append(rand)
# give user feedback
print "%d --> %d" % (count, rand)
count += 1
print rand_list
"""my rand_list 0f 100 random numbers between 0 and 99 -->
[6, 40, 3, 6, 93, 81, 84, 56, 43, 31, 34, 21, 9, 96, 84, 87, 6, 9,
12, 0, 87, 59, 31, 18, 6, 9, 96, 68, 56, 59, 31, 3, 75, 62, 34, 21,
9, 96, 68, 56, 43, 0, 71, 59, 31, 34, 6, 78, 65, 37, 25, 96, 84, 71,
59, 46, 34, 6, 9, 81, 53, 40, 28, 84, 56, 43, 65, 84, 87, 59, 62,
50, 21, 9, 96, 84, 71, 59, 46, 18, 90, 93, 81, 84, 71, 59, 31, 34,
53, 40, 46, 3, 75, 75, 50, 6, 65, 21, 87, 15]
"""
I made the rand_list relatively short for this example, normally you may want to go for a length of >1000.
Edited
by vegaseat: n/a
ov3rcl0ck 25
Right now i have used time for my input. Could you please give a bit more info on used memory and mouse movement?
Fetching memory usage and mouse input are OS dependent. There isn't a library to fetch memory usage, but you can use the "mem" command on windows to fetch it, and the "ps" or "free" command on UNIX to fetch it, but keep in mind you might need to use a little regex or at least some string functions. For mouse input you can use pyHook for windows, and xlib for UNIX(OS X supports X11, and any Unix distro worth using supports it).
If you need me to I can post some examples of pyHook, xlib, and memory fetching.
sanchitgarg
hey thanks everybody for the help.
i wud be using the Linear congruential generator(LCG) only for implementing my code.
thanks again
vegaseat 1,720
hey thanks everybody for the help.
i wud be using the Linear congruential generator(LCG) only for implementing my code.thanks again
Well, once you have it down to Python code, let us know.
sanchitgarg
Well, once you have it down to Python code, let us know.
yes sir!!