| | |
Generating random nos without using random module
Thread Solved |
0
#2 Nov 4th, 2009
•
•
•
•
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.
The general formula for LCG is
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 11:05 am.
0
#3 Nov 4th, 2009
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.
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 12:13 pm.
May 'the Google' be with you!
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#4 Nov 4th, 2009
•
•
•
•
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.
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#5 Nov 4th, 2009
•
•
•
•
Well there are several algorithms to generate random number, simplest to my knowledge being Linear congruential generator(LCG).
The general formula for LCG is. 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?
0
#6 Nov 4th, 2009
You mean output sort of this
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
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 3:29 pm.
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#7 Nov 5th, 2009
•
•
•
•
You mean output sort of this132434454545435435L.
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 doprint num, L doesnt shows up.
print num means? Could you please tell the syntax. I want to print a list of nos.
0
#8 Nov 5th, 2009
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)
for x in rand_num: print x
•
•
Join Date: Sep 2009
Posts: 113
Reputation:
Solved Threads: 22
0
#10 Nov 5th, 2009
•
•
•
•
Well there are several algorithms to generate random number, simplest to my knowledge being Linear congruential generator(LCG).
The general formula for LCG is. 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)
NOTE: sudo doesn't apply to real life situations.
![]() |
Similar Threads
- Generating a random card from a deck with python. (Python)
- display random images from folder (PHP)
- Random rotation of a random vector (C++)
- Pascal random numbers are not random (Pascal and Delphi)
- Compile time errors in C++ while generating random numbers (C++)
- Logic behind generating random number (C)
- problem in generating non repeated random numbers (C)
- Random numbers...really random?? (Java)
Other Threads in the Python Forum
- Previous Thread: How to use MD5 in Python?
- Next Thread: how to use getpass in python
Views: 761 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for Python
application array beginner c++ c/c++ change character class client code command convert count create csv ctypes database dictionary django dll editing error examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming progressbar py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote scrolledtext server socket ssh stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode user variable variables web windows wxpython






