944,153 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1325
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 4th, 2009
0

Generating random nos without using random module

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009
Nov 4th, 2009
0
Re: Generating random nos without using random module
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)
Last edited by vishesh; Nov 4th, 2009 at 12:05 pm.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Nov 4th, 2009
0
Re: Generating random nos without using random module
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.
Last edited by vegaseat; Nov 4th, 2009 at 1:13 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 4th, 2009
0
Re: Generating random nos without using random module
Click to Expand / Collapse  Quote originally posted by vegaseat ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009
Nov 4th, 2009
0
Re: Generating random nos without using random module
Click to Expand / Collapse  Quote originally posted by vishesh ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009
Nov 4th, 2009
0
Re: Generating random nos without using random module
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.
Last edited by vishesh; Nov 4th, 2009 at 4:29 pm.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Nov 5th, 2009
0
Re: Generating random nos without using random module
Click to Expand / Collapse  Quote originally posted by vishesh ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009
Nov 5th, 2009
0
Re: Generating random nos without using random module
What I mean is instead of directly printing whole list, you have to print numbers individually, to avoid L at end:
Python Syntax (Toggle Plain Text)
  1. for x in rand_num:
  2. print x
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Nov 5th, 2009
0
Re: Generating random nos without using random module
Is there any other method or logic except that formula?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanchitgarg is offline Offline
14 posts
since Oct 2009
Nov 5th, 2009
0
Re: Generating random nos without using random module
Click to Expand / Collapse  Quote originally posted by vishesh ...
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).
Reputation Points: 35
Solved Threads: 22
Junior Poster
ov3rcl0ck is offline Offline
113 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How to use MD5 in Python?
Next Thread in Python Forum Timeline: how to use getpass in python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC