Generating random nos without using random module

Thread Solved

Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster

Generating random nos without using random module

 
0
  #1
25 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
25 Days Ago
Originally Posted by sanchitgarg View 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.
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; 25 Days Ago at 12:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #3
25 Days Ago
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; 25 Days Ago at 1:13 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster
 
0
  #4
25 Days Ago
Originally Posted by vegaseat View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster
 
0
  #5
25 Days Ago
Originally Posted by vishesh View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso
 
0
  #6
25 Days Ago
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; 25 Days Ago at 4:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster
 
0
  #7
24 Days Ago
Originally Posted by vishesh View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso
 
0
  #8
24 Days Ago
What I mean is instead of directly printing whole list, you have to print numbers individually, to avoid L at end:
  1. for x in rand_num:
  2. print x
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: sanchitgarg is an unknown quantity at this point 
Solved Threads: 0
sanchitgarg sanchitgarg is offline Offline
Newbie Poster
 
0
  #9
24 Days Ago
Is there any other method or logic except that formula?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 100
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 11
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster
 
0
  #10
24 Days Ago
Originally Posted by vishesh View Post
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).
NOTE: sudo doesn't apply to real life situations.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC